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);
}
}
মেমরি মেট্রিক্স ব্যাখ্যা
| মেট্রিক | সোর্স | অর্থ |
|---|---|---|
| availMem | ActivityManager | সিস্টেমে উপলব্ধ মেমরি (নতুন প্রসেসের জন্য) |
| totalMem | ActivityManager | ডিভাইসের মোট র্যাম |
| threshold | ActivityManager | लो মেমরি কিলার ট্রিগার থ্রেশহোল্ড |
| 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
`