【问题标题】:Runime.getRuntime().totalMemory() and ActivityManager.MemoryInfo()Runime.getRuntime().totalMemory() 和 ActivityManager.MemoryInfo()
【发布时间】:2018-12-15 10:34:53
【问题描述】:

为什么从getRuntime().totalMemory() 提取的总内存与使用ActivityManager.MemoryInfo() 时不相等?在下面的两个代码部分中,我得到了不同的值:

long totalMemory = Runtime.getRuntime().totalMemory() ;

ActivityManager actManager = (ActivityManager) getActivity().getSystemService(ACTIVITY_SERVICE);
                ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
                actManager.getMemoryInfo(memInfo);
                long totalMemory = memInfo.totalMem

在第一个代码中我得到 12.759.040,从第二个代码中我得到 907.034.624!

【问题讨论】:

    标签: java android android-memory


    【解决方案1】:

    这是两个不同的东西。

    Runtime.getRuntime().totalMemory()

    返回 Java 虚拟机中的内存总量。这个值会随着时间而改变。这是运行时可用内存。

    memInfo.totalMem

    返回可用内存总量

    您可以参考这两个 SO 主题:

    Topic 1

    Topic 2

    【讨论】:

    • 感谢两个主题。看到两个话题。两个有用的主题。
    【解决方案2】:

    Runtime.getRuntime().totalMemory()

    返回 Java 虚拟机中的内存总量。此方法返回的值可能会随时间变化,具体取决于宿主环境。

    memInfo.totalMem

    内核可访问的总内存。这基本上是设备的 RAM 大小,不包括内核下的固定分配,如 DMA 缓冲区、基带 CPU 的 RAM 等。

    请注意,保存任何给定类型的对象所需的内存量可能取决于实现。

    • 第一个是运行您的进程的 jvm 所拥有的内存,大约 12 MB。
    • 第二个是内核可访问的系统内存总量,在您的情况下约为 1 GB。

    第一个是第二个的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多