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?
Asked
Active
Viewed 3,830 times
5
-
1Look at http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android and http://kohlerm.blogspot.in/2009/04/analyzing-memory-usage-off-your-android.html – user370305 Jul 09 '12 at 06:03
2 Answers
3
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);

Manoj
- 3,947
- 9
- 46
- 84

user1445716
- 442
- 6
- 18
-
Why the division by 1024? Acording to Android Docs the results are in kB (multiple of 1000) and NOT in KB or KIB. https://developer.android.com/reference/android/os/Debug.MemoryInfo.html https://en.wikipedia.org/wiki/Kilobyte – chris Dec 23 '17 at 03:11
2
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();
}
}
}
you will get String as:
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

ρяσѕρєя K
- 132,198
- 53
- 198
- 213
-
I dont understand what %mem numbers you are showing as an expected output. – user1445716 Jul 09 '12 at 08:07
-
@user1445716 : %MEM means memory used by a process for example `com.android.browser` currently using 72.3 memory and com.android.launcher currently using 64.4 – ρяσѕρєя K Jul 09 '12 at 08:11
-
@user1445716 : but i'm not sure 87.7,72.3 values in % or any other format so i think you can google for it – ρяσѕρєя K Jul 09 '12 at 08:13
-
But 72.3% + 87.7% is greater than 100%... Thats what I dont under stand what the value means. I guess I am looking for what the app is currently using on the heap. I know what is available on the heap, and I know what I should use, the piece missing is what am I currently using on the heap? – user1445716 Jul 09 '12 at 08:16