কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে অ্যাপ্লিকেশন হিপ সাইজ ডিটেক্ট করা

ActivityManager দিয়ে হিপ মেমরি লিমিট জানা

এই উদাহরণটি দেখাবে কীভাবে ActivityManager.getMemoryClass() ব্যবহার করে আপনার অ্যাপের জন্য আশাবাদী হিপ সাইজ লিমিট (MB-তে) বের করা যায়।

পদক্ষেপ ১: প্রজেক্ট তৈরি করুন

Android Studio-তে নতুন প্রজেক্ট শুরু করুন।

পদক্ষেপ ২: লেআউট XML (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="8dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnHeapSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showHeapSize"
        android:text="Detect Heap Size" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textStyle="bold" />

</LinearLayout>

পদক্ষেপ ৩: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
    }

    public void showHeapSize(View view) {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        
        if (activityManager != null) {
            // ডিভাইস-প্রতি অ্যাপ হিপ লিমিট (MB)
            int memoryClass = activityManager.getMemoryClass();
            
            // লার্জ হিপ সক্ষম থাকলে (android:largeHeap="true")
            int largeMemoryClass = activityManager.getLargeMemoryClass();
            
            String info = String.format(
                "Normal Heap Limit: %d MB\nLarge Heap Limit: %d MB",
                memoryClass, largeMemoryClass
            );
            
            textView.setText(info);
        }
    }
}

Manifest-এ Large Heap সক্ষম করা (যদি দরকার হয়)

<application
    android:largeHeap="true"
    ... >
    <activity ...></activity>
</application>

গুরুত্বপূর্ণ ধারণা ও সতর্কতা

  • getMemoryClass(): ডিভাইসের স্ট্যান্ডার্ড হিপ লিমিট (সাধারণত ১৬-২৫৬ MB)
  • getLargeMemoryClass(): largeHeap=true থাকলে লিমিট (সাধারণত ২৫৬-৫১২ MB)
  • এটি লিমিট, বর্তমান ইউজ নয়: বর্তমান হিপ ইউজ জানার জন্য Debug.getNativeHeapAllocatedSize() বা profiler ব্যবহার করুন
  • largeHeap অপ্টিমাইজেশন: শুধু জরুরি হলে (বড় ইমেজ প্রসেসিং, ম্যাপ) ব্যবহার করুন। এটা GC পজ লম্বা করে।

রানটাইম হিপ ইনফো (API 16+)

Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);

long totalPss = memoryInfo.getTotalPss(); // KB
long dalvikPss = memoryInfo.dalvikPss;
long nativePss = memoryInfo.nativePss;

Profiler ব্যবহার (সেরা পদ্ধতি)

Android Studio Profiler (View → Tool Windows → Profiler) ব্যবহার করুন রিয়েল-টাইম হিপ ডাম্প, এলোকেশন ট্র্যাকিং, GC ট্রিগার করার জন্য।

OOM (OutOfMemoryError) প্রতিরোধ

  • inSampleSize দিয়ে বড় ইমেজ লোড করুন
  • LruCache ব্যবহার করুন Bitmap ক্যাশিংয়ের জন্য
  • Glide/Picasso/Coil লাইব্রেরি ব্যবহার করুন
  • WeakReference ব্যবহার করুন ক্যাশে-এ
  1. অ্যান্ড্রয়েডে ButterKnife লাইব্রেরি ব্যবহারের পদ্ধতি

  2. অ্যান্ড্রয়েডে JSON পার্স করার সহজ পদ্ধতি

  3. Android-এ ScrollView-এর শেষে পৌঁছানোর ইভেন্ট ধরার পদ্ধতি

  4. Android-এ TextView-এ টেক্সট জাস্টিফাই (Justify) করার পদ্ধতি