एंड्रॉइड में ActivityManager से हीप वॉच लिमिट क्लियर करना
यह ट्यूटोरियल दिखाता है कि ActivityManager.clearWatchHeapLimit() का उपयोग करके पहले से सेट हीप वॉच लिमिट को कैसे क्लियर करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट (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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity कोड (java/MainActivity.java)
package com.example.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N) // API 24+
@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);
// हीप वॉच लिमिट क्लियर करें
activityManager.clearWatchHeapLimit();
textView.setText("हीप वॉच लिमिट क्लियर हो गई");
}
}
हीप वॉच लिमिट क्या है?
- डिबगिंग टूल: जब ऐप की हीप उपयोग एक सीमा पार करती है तो सिस्टम नोटिफाई करता है
setWatchHeapLimit(bytes)से सेट किया जाता है- लिमिट पार होने पर
onTrimMemory()या हीप डंप ट्रिगर हो सकता है - मुख्य रूप से डेवलपमेंट/प्रोफाइलिंग के लिए
संबंधित API
// लिमिट सेट करें (बाइट्स में) - अगला आर्टिकल देखें activityManager.setWatchHeapLimit(4 * 1024 * 1024); // 4MB // वर्तमान हीप जानकारी Debug.MemoryInfo info = new Debug.MemoryInfo(); Debug.getMemoryInfo(info); long dalvikPss = info.dalvikPss; long nativePss = info.nativePss; // मेमोरी क्लास (ऐप प्रति अधिकतम हीप) int memoryClass = activityManager.getMemoryClass(); // MB में int largeMemoryClass = activityManager.getLargeMemoryClass();
व्यावहारिक उपयोग
// डिबग बिल्ड में हीप मॉनिटरिंग
if (BuildConfig.DEBUG) {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
// 50MB लिमिट सेट करें
am.setWatchHeapLimit(50 * 1024 * 1024);
// बाद में क्लियर करें
// am.clearWatchHeapLimit();
}
नोट
- API 24 (Nougat) से उपलब्ध
- आमतौर पर प्रोडक्शन कोड में उपयोग नहीं किया जाता
- प्रोफाइलिंग के लिए Android Studio Profiler बेहतर है
- DevicePolicyManager संदर्भ मूल कोड में गलती से शामिल था
ऐप टेस्ट करें
चलाएं - "हीप वॉच लिमिट क्लियर हो गई" संदेश दिखेगा। कोई विजुअल परिवर्तन नहीं क्योंकि यह डिबगिंग API है।