我想通过两种方式了解我的 android 应用程序使用的堆空间量: - 以编程方式 - 通过 DDMS。
我提到了this发布,在此处发布之前。在那篇文章中,提到 Debug.getNativeHeapSize()
返回堆大小。这是我应该使用的确切方法,以便以编程方式检测堆大小吗?如果是这样,我应该在哪里记录它才能正确使用我的应用程序的堆大小?
最佳答案
这是我使用的:
public static void logHeap() {
Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
Double available = new Double(Debug.getNativeHeapSize())/1048576.0;
Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
Log.d("tag", "debug. =================================");
Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");
Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
}
关于java - 如何检测 android 应用程序的堆大小使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820353/