I want to get full RAM size of a device. memoryInfo.getTotalPss() returns 0. There is not function for get total RAM size in ActivityManager.MemoryInfo.
How to do this?
I want to get full RAM size of a device. memoryInfo.getTotalPss() returns 0. There is not function for get total RAM size in ActivityManager.MemoryInfo.
How to do this?
As of API level 16 you can now use the totalMem property of the MemoryInfo class.
Like this:
ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;
Api level 15 and lower still requires to use the unix command as shown in cweiske's answer.
Standard unix command: $ cat /proc/meminfo
Note that /proc/meminfo is a file. You don't actually have to run cat, you can simply read the file.
I can get the usable RAM memory in such a way
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;
}
Tested Upto Android 4.3 : SAMSUNG S3
You can get the total RAM size by using this code:
var activityManager = GetSystemService(Activity.ActivityService) as ActivityManager; var memoryInfo = new ActivityManager.MemoryInfo(); activityManager.GetMemoryInfo(memoryInfo); var totalRam = memoryInfo.TotalMem / (1024 * 1024);
If the device has 1GB RAM, totalRam will be 1000.
To get RAM values simply do:
ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
assert actManager != null;
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;
long availMemory = memInfo.availMem;
long usedMemory = totalMemory - availMemory;
float precentlong = (((float) (availMemory / totalMemory)) * 100);
Here you will get Total as well as Free and Used RAM size. The value of these will be in "long", so format it to human-readable (i.e in MB/GB). Use the following method to do so:
private String floatForm(double d) {
return String.format(java.util.Locale.US, "%.2f", d);
}
private String bytesToHuman(long size) {
long Kb = 1024;
long Mb = Kb * 1024;
long Gb = Mb * 1024;
long Tb = Gb * 1024;
long Pb = Tb * 1024;
long Eb = Pb * 1024;
if (size < Kb) return floatForm(size) + " byte";
if (size >= Kb && size < Mb) return floatForm((double) size / Kb) + " KB";
if (size >= Mb && size < Gb) return floatForm((double) size / Mb) + " MB";
if (size >= Gb && size < Tb) return floatForm((double) size / Gb) + " GB";
if (size >= Tb && size < Pb) return floatForm((double) size / Tb) + " TB";
if (size >= Pb && size < Eb) return floatForm((double) size / Pb) + " Pb";
if (size >= Eb) return floatForm((double) size / Eb) + " Eb";
return "0";
}
So now for setting these value to any textview do:
totalRam_tv.setText("".concat(bytesToHuman(totalMemory)));
Simple method to get the total and available RAM are given below:
//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();
Hope this will work.
For formatting the value to KB and MB, the following method can be used :
/**
* 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();
}
The method body can be customised to get desirable results.