Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีล้าง Heap Watch Limit ใน Android

ภาพรวม

บทความนี้สาธิตวิธีล้าง (Clear) Heap Watch Limit ที่ตั้งไว้ผ่าน ActivityManager.clearWatchHeapLimit()

พื้นหลัง: Heap Watch Limit คืออะไร?

Heap Watch Limit เป็นการตั้งค่าให้ระบบแจ้งเตือน (Callback) เมื่อ Heap Memory ของ Process เกินค่าที่กำหนด ใช้สำหรับ Debug memory leak

ขั้นตอนที่ 1: สร้างโปรเจกต์

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

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

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center" />

</LinearLayout>

ขั้นตอนที่ 3: โค้ดหลัก (java/MainActivity.java)

package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        // ล้าง Watch Heap Limit ที่ตั้งไว้ก่อนหน้า
        activityManager.clearWatchHeapLimit();

        textView.setText("Heap Watch Limit ถูกล้างแล้ว");
    }
}

คู่กับ setWatchHeapLimit()

ปกติใช้คู่กัน:

// ตั้งค่า Watch Limit (เช่น 4 MB)
activityManager.setWatchHeapLimit(4 * 1024 * 1024); // 4 MB

// ... ทำงานที่ต้องการ monitor memory ...

// ล้าง Watch Limit เมื่อเสร็จ
activityManager.clearWatchHeapLimit();

ข้อจำกัดและสิทธิ์

  • ต้องการ Permission: android.permission.SET_ACTIVITY_WATCHER (Signature|System level - แอปทั่วไปใช้ไม่ได้)
  • ใช้ได้เฉพาะ System Apps หรือ แอปที่ลงนามด้วย Platform Key
  • สำหรับแอปทั่วไป ใช้ Debug.getNativeHeapAllocatedSize() หรือ Runtime.getRuntime().totalMemory() แทน

ทางเลือกสำหรับแอปทั่วไป

// ตรวจสอบ Memory ใช้เอง
Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
long nativePss = memoryInfo.getTotalPss() * 1024; // KB to bytes
long dalvikPss = memoryInfo.dalvikPss * 1024;

// หรือใช้ Runtime
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long usedMemory = totalMemory - freeMemory;

ทดสอบ

รันแอป (บน System App หรือ Emulator root) จะแสดง "Heap Watch Limit ถูกล้างแล้ว"