Ich möchte die volle RAM Größe eines Geräts erhalten. memoryInfo.getTotalPss()
gibt 0 zurück. Es gibt keine Funktion zum Abrufen der Gesamtgröße von RAM in ActivityManager.MemoryInfo
.
Wie macht man das?
Standard Unix-Befehl: $ cat /proc/meminfo
Beachten Sie, dass /proc/meminfo
eine Datei ist. Sie müssen cat
nicht wirklich ausführen, Sie können die Datei einfach lesen.
Ab API-Ebene 16 können Sie nun die totalMem
-Eigenschaft der MemoryInfo
-Klasse verwenden.
So was:
ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;
Für API-Level 15 und niedriger muss noch der Unix-Befehl verwendet werden, wie in cweiskes Antwort gezeigt.
Ich kann so den nutzbaren RAM Speicher erhalten
public String getTotalRAM() {
RandomAccessFile reader = null;
String load = null;
DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
double totRam = 0;
String lastValue = "";
try {
reader = new RandomAccessFile("/proc/meminfo", "r");
load = reader.readLine();
// Get the Number value from the string
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(load);
String value = "";
while (m.find()) {
value = m.group(1);
// System.out.println("Ram : " + value);
}
reader.close();
totRam = Double.parseDouble(value);
// totRam = totRam / 1024;
double mb = totRam / 1024.0;
double gb = totRam / 1048576.0;
double tb = totRam / 1073741824.0;
if (tb > 1) {
lastValue = twoDecimalForm.format(tb).concat(" TB");
} else if (gb > 1) {
lastValue = twoDecimalForm.format(gb).concat(" GB");
} else if (mb > 1) {
lastValue = twoDecimalForm.format(mb).concat(" MB");
} else {
lastValue = twoDecimalForm.format(totRam).concat(" KB");
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// Streams.close(reader);
}
return lastValue;
}
Getestet bis Android 4.3: SAMSUNG S3
Sie können die Gesamtgröße von RAM mithilfe des folgenden Codes ermitteln:
var activityManager = GetSystemService (Activity.ActivityService) als ActivityManager; var memoryInfo = neuer ActivityManager.MemoryInfo (); activityManager.GetMemoryInfo (memoryInfo); var totalRam = memoryInfo.TotalMem/(1024 * 1024);
Wenn das Gerät über 1 GB RAM verfügt, beträgt totalRam 1000.
Eine einfache Methode zum Abrufen der Gesamtzahl und der verfügbaren RAM sind unten angegeben:
//Method call returns the free RAM currently and returned value is in bytes.
Runtime.getRuntime().freeMemory();
//Method call returns the total RAM currently and returned value is in bytes.
Runtime.getRuntime().maxMemory();
Hoffe das wird funktionieren.
Zum Formatieren des Werts in KB und MB kann die folgende Methode verwendet werden:
/**
* Method to format the given long value in human readable value of memory.
* i.e with suffix as KB and MB and comma separated digits.
*
* @param size Total size in long to be formatted. <b>Unit of input value is assumed as bytes.</b>
* @return String the formatted value. e.g for input value 1024 it will return 1KB.
* <p> For the values less than 1KB i.e. same input value will return back. e.g. for input 900 the return value will be 900.</p>
*/
private String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = " KB";
size /= 1024;
if (size >= 1024) {
suffix = " MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null) resultBuffer.append(suffix);
return resultBuffer.toString();
}
Der Methodenkörper kann angepasst werden, um die gewünschten Ergebnisse zu erzielen.