我把它放在 Application 类的 Create() 的第一行:
@Override
public void onCreate() {
Log.i("test", "application oncreate");
showMemoryStats();
super.onCreate();
}
其中 showMemoryStats() 是:
public static void showMemoryStats() {
double nativeUsage = Debug.getNativeHeapAllocatedSize();
Log.i("test", "nativeUsage: " + nativeUsage);
//current heap size
double heapSize = Runtime.getRuntime().totalMemory();
Log.i("test", "heapSize: " + heapSize);
//amount available in heap
double heapRemaining = Runtime.getRuntime().freeMemory();
Log.i("test", "heapRemaining: " + heapRemaining);
double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
Log.i("test", "memoryAvailable: " + memoryAvailable);
}
我在完全卸载后启动应用程序。我得到:
07-03 17:39:05.212: I/test(19016): nativeUsage: 5649968.0
07-03 17:39:05.212: I/test(19016): heapSize: 4710384.0
07-03 17:39:05.212: I/test(19016): heapRemaining: 1705056.0
07-03 17:39:05.212: I/test(19016): memoryAvailable: 2.4899136E7
这在本机堆中已经是 5 mb,为什么?这正常吗?