首页 > 解决方案 > Runime.getRuntime().totalMemory() 和 ActivityManager.MemoryInfo()

问题描述

为什么提取的总内存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 !

标签: javaandroidandroid-memory

解决方案


这是两个不同的东西。

Runtime.getRuntime().totalMemory()

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

memInfo.totalMem

返回可用内存的总量

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

话题一

话题二


推荐阅读