บทความนี้สาธิตวิธีดึงข้อมูล Memory Usage ของแอป Android โดยใช้ ActivityManager.MemoryInfo และ Runtime class
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?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="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java
import android.app.ActivityManager;
import android.content.Context;
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);
TextView textView = findViewById(R.id.textView);
textView.setText(getMemoryInfo());
}
private String getMemoryInfo() {
// 1. System-wide memory info
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(memoryInfo);
// 2. App-specific runtime memory
Runtime runtime = Runtime.getRuntime();
StringBuilder sb = new StringBuilder();
sb.append("=== System Memory (ActivityManager) ===\n");
sb.append(String.format("Available Memory: %,d KB (%.1f MB)\n", memoryInfo.availMem / 1024, memoryInfo.availMem / 1024f / 1024f));
sb.append(String.format("Total Memory: %,d KB (%.1f MB)\n", memoryInfo.totalMem / 1024, memoryInfo.totalMem / 1024f / 1024f));
sb.append(String.format("Low Memory: %s\n", memoryInfo.lowMemory ? "Yes" : "No"));
sb.append(String.format("Threshold: %,d KB\n\n", memoryInfo.threshold / 1024));
sb.append("=== App Runtime Memory (Runtime) ===\n");
sb.append(String.format("Max Memory: %,d KB (%.1f MB)\n", runtime.maxMemory() / 1024, runtime.maxMemory() / 1024f / 1024f));
sb.append(String.format("Total Memory: %,d KB (%.1f MB)\n", runtime.totalMemory() / 1024, runtime.totalMemory() / 1024f / 1024f));
sb.append(String.format("Free Memory: %,d KB (%.1f MB)\n", runtime.freeMemory() / 1024, runtime.freeMemory() / 1024f / 1024f));
sb.append(String.format("Used Memory: %,d KB (%.1f MB)\n", (runtime.totalMemory() - runtime.freeMemory()) / 1024, (runtime.totalMemory() - runtime.freeMemory()) / 1024f / 1024f));
return sb.toString();
}
}
ความแตกต่าง: ActivityManager vs Runtime
| Class | ขอบเขต | ค่าสำคัญ |
|---|---|---|
ActivityManager.MemoryInfo | System-wide (ทั้งอุปกรณ์) | availMem, totalMem, lowMemory, threshold |
Runtime | Process-specific (แอปนี้อย่างเดียว) | maxMemory(), totalMemory(), freeMemory() |
คำอธิบายค่า Runtime
- maxMemory() - หน่วยความจำสูงสุดที่แอปได้รับ (Heap limit) ~ 128-512MB ขึ้นอยู่กับอุปกรณ์
- totalMemory() - Heap ปัจจุบันที่จองไว้ (เพิ่มขึ้นตามการใช้งาน)
- freeMemory() - พื้นที่ว่างใน Heap ปัจจุบัน
- Used = totalMemory() - freeMemory() - หน่วยความจำที่แอปใช้จริงอยู่
เครื่องมือ Debug ที่ดีกว่า (Production)
1. Android Studio Profiler
View → Tool Windows → Profiler → Memory Tab - ดูกราฟ Real-time, Heap Dump, Allocation Tracking
2. Debug API (Programmatic)
Debug.MemoryInfo debugInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(debugInfo); // debugInfo.getTotalPss(), getDalvikPss(), getNativePss(), getOtherPss()
3. ADB Commands
adb shell dumpsys meminfo <package_name> adb shell procrank
Best Practices จัดการ Memory
- หลีกเลี่ยง Memory Leak (Context, Static references, Unregistered listeners)
- ใช้
WeakReferenceสำหรับ Callback/Listener - Recycle Bitmap เมื่อไม่ใช้:
bitmap.recycle()(API < 19) หรือให้ GC จัดการ - ใช้
LruCacheสำหรับ Image Caching - หลีกเลี่ยงสร้าง Object ใน Loop/onDraw