如何检测Android应用程序的堆大小使用情况

use*_*953 21 java android android-widget android-emulator

我想通过两种方式知道我的android应用程序使用的堆空间量: - 以编程方式 - 通过DDMS.

在发布之前我已经提到了这篇文章.在Debug.getNativeHeapSize()那篇文章中,提到了,返回堆大小.这是我应该使用的确切方法,以编程方式检测堆大小?如果是这样,我应该在哪里记录它以获得我的应用程序的正确堆大小使用?

zrg*_*giu 55

这是我用的:

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)");
    }
Run Code Online (Sandbox Code Playgroud)