Memory seems to be a big topic and I cant find the specific answer. I've got the answers on how much is available in the heap and I know how much should I use. I need the answer how to code to programatically determine how much memory is my app using of the heap? And how much total memory am I using?
This works:
Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
String memMessage = String.format("App Memory: Pss=%.2f MB\nPrivate=%.2f MB\nShared=%.2f MB",
memoryInfo.getTotalPss() / 1024.0,
memoryInfo.getTotalPrivateDirty() / 1024.0,
memoryInfo.getTotalSharedDirty() / 1024.0);
Toast.makeText(this,memMessage,Toast.LENGTH_LONG).show();
Log.i("log_tag", memMessage);
use top -d 1 -n 1
Android Shell Command for getting all process list with process names or used memory by processes and then extract your process info from return string from System:
BufferedReader in = null;
try {
Process process = null;
process = Runtime.getRuntime().exec("top -n 1 -d 1");
in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
String line ="";
String content = "";
while((line = in.readLine()) != null) {
content += line + "\n";
}
System.out.println(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PID PPID USER STAT VSZ %MEM %CPU COMMAND
9673 9672 root R 712 0.1 0.0 top -d 1 -n 1
2489 2386 system S 369m 87.7 0.0 system_server
3101 2386 app_23 S 304m 72.3 0.0 com.android.browser
2581 2386 radio S 279m 66.3 0.0 com.android.phone
2585 2386 app_15 S 271m 64.4 0.0 com.android.launcher
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With