Skip to content

Instantly share code, notes, and snippets.

@loopj
Created May 29, 2012 00:51
Show Gist options
  • Save loopj/2821941 to your computer and use it in GitHub Desktop.
Save loopj/2821941 to your computer and use it in GitHub Desktop.
Android memory metrics
// Device memory data
ActivityManager activityManager = (ActivityManager) androidContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
// Runtime data memory numbers are in bytes (dalvik process?)
int processors = runtime.availableProcessors();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println(String.format("Runtime: %d processors, %d totalMemory (bytes), %d freeMemory (bytes)", processors, totalMemory, freeMemory));
// Debug data (system wide) memory numbers are in bytes
long nativeHeapSize = Debug.getNativeHeapSize();
long nativeHeapAllocatedSize = Debug.getNativeHeapAllocatedSize();
long nativeHeapFreeSize = Debug.getNativeHeapFreeSize();
System.out.println(String.format("Debug: %d nativeHeapSize (bytes), %d nativeHeapAllocatedSize (bytes), %d nativeHeapFreeSize (bytes)", nativeHeapSize, nativeHeapAllocatedSize, nativeHeapFreeSize));
// Debug memoryinfo (process specific) memory numbers are in kb
Debug.MemoryInfo debugMemoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(debugMemoryInfo);
System.out.println(String.format("Debug.MemoryInfo dalvik: %d pss, %d shared, %d private", debugMemoryInfo.dalvikPss, debugMemoryInfo.dalvikSharedDirty, debugMemoryInfo.dalvikPrivateDirty));
System.out.println(String.format("Debug.MemoryInfo native: %d pss, %d shared, %d private", debugMemoryInfo.nativePss, debugMemoryInfo.nativeSharedDirty, debugMemoryInfo.nativePrivateDirty));
System.out.println(String.format("Debug.MemoryInfo other: %d pss, %d shared, %d private", debugMemoryInfo.otherPss, debugMemoryInfo.otherSharedDirty, debugMemoryInfo.otherPrivateDirty));
// Device "memory class", this is how much ram each app is allowed in MB
int memoryClass = activityManager.getMemoryClass();
System.out.println(String.format("ActivityManager: %d memoryClass", memoryClass));
// Total device memory stats in bytes
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
System.out.println(String.format("ActivityManager.memoryInfo %d availMem, %b lowMemory, %d threshold", memoryInfo.availMem, memoryInfo.lowMemory, memoryInfo.threshold));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment