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

অ্যান্ড্রয়েড অ্যাপের RAM ব্যবহার পরিমাপ করার পদ্ধতি

ActivityManager.MemoryInfo এবং Runtime ক্লাস ব্যবহার করে মেমরি মনিটরিং

অ্যান্ড্রয়েড অ্যাপের মেমরি ব্যবহার (RAM) জানতে `ActivityManager.MemoryInfo` (সিস্টেম लेভেল) এবং `Runtime` ক্লাস (JVM হিপ লেভেল) ব্যবহার করা হয়। এটি পারফরম্যান্স অপ্টিমাইজেশন এবং মেমরি লিক ডিটেকশনে সাহায্য করে।

ধাপ ১: প্রজেক্ট তৈরি করুন

স্ট্যান্ডার্ড অ্যান্ড্রয়েড প্রজেক্ট।

ধাপ ২: লেআউট

res/layout/activity_main.xml - একটি TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="8dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ধাপ ৩: মেমরি ইনফো লজিক

src/MainActivity.java:

import android.app.ActivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final TextView textView = findViewById(R.id.text);
        textView.setText(getMemoryInfo());
    }
    
    private String getMemoryInfo() {
        // সিস্টেম লেভেল মেমরি ইনফো
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(memoryInfo);
        
        // JVM হিপ মেমরি ইনফো
        Runtime runtime = Runtime.getRuntime();
        
        StringBuilder builder = new StringBuilder();
        builder.append("Available Memory: ").append(formatBytes(memoryInfo.availMem)).append("\n")
               .append("Total System Memory: ").append(formatBytes(memoryInfo.totalMem)).append("\n")
               .append("Threshold: ").append(formatBytes(memoryInfo.threshold)).append("\n\n")
               .append("Runtime Max Memory: ").append(formatBytes(runtime.maxMemory())).append("\n")
               .append("Runtime Total Memory: ").append(formatBytes(runtime.totalMemory())).append("\n")
               .append("Runtime Free Memory: ").append(formatBytes(runtime.freeMemory())).append("\n")
               .append("Runtime Used Memory: ").append(formatBytes(runtime.totalMemory() - runtime.freeMemory()));
        
        return builder.toString();
    }
    
    private String formatBytes(long bytes) {
        if (bytes < 1024) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        char unit = "KMGTPE".charAt(exp - 1);
        return String.format("%.2f %sB", bytes / Math.pow(1024, exp), unit);
    }
}

মেমরি মেট্রিক্স ব্যাখ্যা

মেট্রিকসোর্সঅর্থ
availMemActivityManagerসিস্টেমে উপলব্ধ মেমরি (নতুন প্রসেসের জন্য)
totalMemActivityManagerডিভাইসের মোট র‍্যাম
thresholdActivityManagerलो মেমরি কিলার ট্রিগার থ্রেশহোল্ড
maxMemory()Runtimeঅ্যাপের জন্য সর্বোচ্চ হিপ সাইজ
totalMemory()Runtimeবর্তমান আলোকিত হিপ সাইজ
freeMemory()Runtimeহিপে খালি স্পেস

উন্নত মেমরি মনিটরিং

// অ্যাপের নিজস্ব PSS (Proportional Set Size) পাওয়া
Debug.MemoryInfo[] memoryInfo = new Debug.MemoryInfo[1];
Debug.getMemoryInfo(memoryInfo);
long pss = memoryInfo[0].getTotalPss() * 1024; // KB to bytes

// কম্পোনেন্ট-ভিত্তিক মেমরি
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processes) {
    if (process.pkgList[0].equals(getPackageName())) {
        int[] pids = new int[]{process.pid};
        Debug.MemoryInfo[] infos = activityManager.getProcessMemoryInfo(pids);
        // infos[0] দিয়ে ডিটেইলড মেমরি
    }
}

প্রোফাইলিং টুলস

  • Android Studio Profiler - রিয়েল-টাইম মেমরি গ্রাফ
  • LeakCanary - মেমরি লিক ডিটেকশন লাইব্রেরি
  • dumpsys meminfo - কমান্ড লাইন টুল: `adb shell dumpsys meminfo `
অ্যান্ড্রয়েড অ্যাপের RAM ব্যবহার পরিমাপ করার পদ্ধতি
  1. অ্যান্ড্রয়েডে Spinner-এর টেক্সট আকার ও রঙ পরিবর্তন করার পদ্ধতি

  2. Android-এ Home Button ওভাররাইড/হ্যান্ডল করার পদ্ধতি

  3. অ্যান্ড্রয়েড সিস্টেম ভার্সন চেক করার পদ্ধতি

  4. Android অ্যাপ্লিকেশন ব্যাকগ্রাউন্ডে চলছে কিনা তা যাচাই করার পদ্ধতি