java - 如何检测 android 应用程序的堆大小使用情况

标签 java android android-emulator android-widget

我想通过两种方式了解我的 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/

相关文章:

java - 无法在 Android 模拟器上 root

java - android 文件输入流 EOF

android - 是否可以从外部aar库覆盖android字符串资源?

Android 接收器 - 多个应用程序中接收器的名称和操作相同

android - OnePlus Espresso 测试运行问题 (Android Studio 3.6.2)

android - 限制用户在编辑文本中输入 1 个十进制值

android - 无法在 Android 模拟器中读取图像

java 网页本地化支持

java - 设置闹钟以在不同时间重复工作日

Java游戏: Keeping framerate the same on all computers