Runtime runtime = Runtime.getRuntime();
Getting the max heap size that the app can use:
//获取可以使用java heap最大值,单位byte
long maxMemory=runtime.maxMemory();
Getting how much of the heap your app can now use (not use memory) :
//获取**申请但未使用**java heap,单位byte
long freeMemory = runtime.freeMemory();
//获取已经申请的全部内存
long usedMemory=runtime.totalMemory() ;
Getting how much of the heap your app currently uses:
//获取当前已经使用java heap,单位byte
long usedMemory=runtime.totalMemory() - runtime.freeMemory();
//当前可以new出来的最大内存,单位byte
long availableMemory = runtime.maxMemory() - runtime.totalMemory()
+ runtime.freeMemory();
And, to format each of them nicely, you can use:
String formattedMemorySize=Formatter.formatShortFileSize(context,memorySize);
*[1]:https://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android