Skip to content

Instantly share code, notes, and snippets.

@wbspry
Created February 28, 2017 06:22
Show Gist options
  • Save wbspry/cf2fe90ab54fba3e917b060db1a595d3 to your computer and use it in GitHub Desktop.
Save wbspry/cf2fe90ab54fba3e917b060db1a595d3 to your computer and use it in GitHub Desktop.
Androidのメモリ情報を取得し文字列にする
@NonNull
private String getMemoryString() {
int memoryClass = ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getMemoryClass();
int largeMemoryClass = ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getLargeMemoryClass();
// メモリ情報を取得
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
int avaliMem = (int) (memoryInfo.availMem / 1024 / 1024);
int threshold = (int) (memoryInfo.threshold / 1024 / 1024);
boolean lowMemory = memoryInfo.lowMemory;
int nativeAllocate = (int) (Debug.getNativeHeapAllocatedSize() / 1024 / 1024);
int dalvikTotal = (int) (Runtime.getRuntime().totalMemory() / 1024 / 1024);
int dalvikFree = (int) (Runtime.getRuntime().freeMemory() / 1024 / 1024);
int javaAllocate = dalvikTotal - dalvikFree;
int totalAllocate = nativeAllocate + javaAllocate;
int ratio = (int)((double) totalAllocate / memoryClass * 100);
int largeRatio = (int)((double) totalAllocate / largeMemoryClass * 100);
return "使用可能メモリ = " + String.valueOf(memoryClass) + " MB\n"
+ "使用可能メモリ(large) = " + largeMemoryClass + " MB\n"
+ "native割当済み = " + nativeAllocate + " MB\n"
+ "java割当済み = " + javaAllocate + " MB\n"
+ "total割当済み = " + totalAllocate + " MB\n"
+ "使用率 = " + ratio + "%\n"
+ "使用率(large) = " + largeRatio + "%\n"
+ "(dalvik最大メモリ = " + dalvikTotal + " MB)\n"
+ "(dalvik空きメモリ = " + dalvikFree + " MB)\n"
+ "availMem = " + avaliMem + " MB\n"
+ "threshold = " + threshold + " MB\n"
+ "lowMemory = " + lowMemory;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment