एंड्रॉइड में ActivityManager से उपलब्ध मेमोरी जानकारी प्राप्त करना
यह ट्यूटोरियल दिखाता है कि ActivityManager.MemoryInfo का उपयोग करके डिवाइस की उपलब्ध मेमोरी (RAM) कैसे प्राप्त करें।
चरण 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="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp" />
</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.JELLY_BEAN_MR2) // API 18+
@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.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// बाइट्स में मान
long availMemBytes = memoryInfo.availMem;
long totalMemBytes = memoryInfo.totalMem;
long thresholdBytes = memoryInfo.threshold;
boolean lowMemory = memoryInfo.lowMemory;
// पठनीय फॉर्मेट में
String formatted = String.format(
"उपलब्ध मेमोरी: %s\n" +
"कुल मेमोरी: %s\n" +
"लो मेमोरी थ्रेशोल्ड: %s\n" +
"लो मेमोरी: %s",
formatBytes(availMemBytes),
formatBytes(totalMemBytes),
formatBytes(thresholdBytes),
lowMemory ? "हां" : "नहीं"
);
textView.setText(formatted);
}
private String formatBytes(long bytes) {
if (bytes < 1024) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(1024));
String unit = "KMGTPE".charAt(exp-1) + "B";
return String.format("%.2f %s", bytes / Math.pow(1024, exp), unit);
}
}
MemoryInfo फील्ड्स
| फील्ड | विवरण |
|---|---|
availMem | ऐप्स के लिए उपलब्ध मेमोरी (बाइट्स) |
totalMem | कुल सिस्टम मेमोरी (API 16+) |
threshold | लो मेमोरी किलर थ्रेशोल्ड |
lowMemory | क्या सिस्टम लो मेमोरी में है |
नोट: यह "एक्टिविटी की मेमोरी" नहीं है
ActivityManager.MemoryInfo पूरे सिस्टम की मेमोरी जानकारी देता है, न कि केवल वर्तमान एक्टिविटी/ऐप की। ऐप-विशिष्ट मेमोरी के लिए:
// ऐप की हीप उपयोग Debug.MemoryInfo debugInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(debugInfo); long dalvikPss = debugInfo.dalvikPss; long nativePss = debugInfo.nativePss; // या Runtime Runtime runtime = Runtime.getRuntime(); long maxMemory = runtime.maxMemory(); // अधिकतम हीप long totalMemory = runtime.totalMemory(); // वर्तमान आवंटित long freeMemory = runtime.freeMemory(); // हीप में फ्री
ऐप टेस्ट करें
चलाएं - उपलब्ध RAM, कुल RAM, थ्रेशोल्ड, और लो मेमोरी स्टेटस MB/GB में दिखेगा।