RAM(Random-access memory),内存,是非常重要的资源,尤其是在手机这样的受限设备上。
不正确的使用会导致内存泄漏、内存抖动等问题,影响app性能及用户体验,因此我们需要对app内存使用状况进行监控。
那么app内存的使用状况应该怎样查看呢?
内存查看工具
先让我们使用Android Studio内置的开发工具Profiler来看一下app的内存使用状况:
我们可以看到红框标出的实时采集的内存状况。
那么这些值分别来自哪里呢?让我们再用dumpsys meminfo来看一下。
adb shell dumpsys meminfo com.jingdong.shperfmonitor
我们可以看到红框里的各项数值基本与之前我们使用Android Studio Profiler获取到的各项数值一一对应。
也就是说,app内存统计的实际是一个叫Pss的值。那么,什么是Pss?旁边的Private Dirty/Clean这些又代表什么?
内存的定义
- Private (Clean and Dirty) RAM
This is memory that is being used by only your process. This is the bulk of the RAM that the system can reclaim when your app’s process is destroyed. Generally, the most important portion of this is private dirty RAM, which is the most expensive because it is used by only your process and its contents exist only in RAM so can’t be paged to storage (because Android does not use swap). All Dalvik and native heap allocations you make will be private dirty RAM; Dalvik and native allocations you share with the Zygote process are shared dirty RAM.
- Proportional Set Size (PSS)
This is a measurement of your app’s RAM use that takes into account sharing pages across processes. Any RAM pages that are unique to your process directly contribute to its PSS value, while pages that are shared with other processes contribute to the PSS value only in proportion to the amount of sharing. For example, a page that is shared between two processes will contribute half of its size to the PSS of each process.
The Dalvik heap is constrained to a single virtual memory range for each app process. This defines the logical heap size, which can grow as it needs to but only up to a limit that the system defines for each app.
The logical size of the heap is not the same as the amount of physical memory used by the heap. When inspecting your app's heap, Android computes a value called the Proportional Set Size (PSS), which accounts for both dirty and clean pages that are shared with other processes—but only in an amount that's proportional to how many apps share that RAM. This (PSS) total is what the system considers to be your physical memory footprint.
Pss(Proportional Set Size)并不等同于实际物理内存的大小,它包含了Private (Clean and Dirty) RAM,并且考虑了内存共享的情况,将共享的内存均摊到每一个参与共享的进程上。因此这个值可以比较准确的表示你的memory footprint。
那么,在app运行时我们如何获得这个值呢?
API
1. High level API
调用ActivityManager#getMemoryInfo()得到的ActivityManager.MemoryInfo中提供了系统当前的内存状况:
- availMem 系统剩余可用内存
- totalMem 系统总内存
- threshold 系统剩余内存阈值,小于这个值后系统开始杀进程
- lowMemory 系统是否处于低内存状态
2. Low level API
调用Debug.getMemoryInfo()或者ActivityManager#getProcessMemoryInfo(int[])得到的Debug.MemoryInfo中提供了进程当前的内存状况,归为dalvik, native, other三类:
/** The proportional set size for dalvik. */
public int dalvikPss;
/** The private dirty pages used by dalvik. */
public int dalvikPrivateDirty;
/** The shared dirty pages used by dalvik. */
public int dalvikSharedDirty;
/** The proportional set size for the native heap. */
public int nativePss;
/** The private dirty pages used by the native heap. */
public int nativePrivateDirty;
/** The shared dirty pages used by the native heap. */
public int nativeSharedDirty;
/** The proportional set size for everything else. */
public int otherPss;
/** The private dirty pages used by everything else. */
public int otherPrivateDirty;
/** The shared dirty pages used by everything else. */
public int otherSharedDirty;
注意:
- Debug.getMemoryInfo()可能无法获取"some protected allocations, such as graphics",没有调用频率限制。
- ActivityManager#getProcessMemoryInfo(int[])可以获取全部信息,但是在Android Q(29)之后,调用频率被限制,超频调用只会看到上次调用的值。
3. 虚拟机内存状况API
Android系统中,通常每个应用运行在一个自己的进程中,而每一个进程都拥有一个独立的虚拟机实例。
通过如下API可以获得应用当前的内存状况:
Runtime runtime = Runtime.getRuntime();
long freeMemory = runtime.freeMemory(); // 应用剩余可用内存
long totalMemory = runtime.totalMemory(); // 应用当前使用的总内存
long maxMemory = runtime.maxMemory(); // 应用最多可以使用的内存
为了保证多任务的正常使用,Android系统对每个app可以使用的内存设置了"硬"上限。
这个值根据设备内存的不同而不同,可以使用上述的Runtime#maxMemory()或者ActivityManager#getMemoryClass()获得。
如果你的应用达到这个值后继续申请内存,就会收到OutOfMemoryError
。
监控方案
根据以上这些API,我们可以实现最终的监控方案:
- app当前占用内存:调用Debug.getMemoryInfo()得到Debug.MemoryInfo,调用其Debug.MemoryInfo#getTotalPss()方法获得该值
- 系统剩余内存:调用ActivityManager#getMemoryInfo()得到ActivityManager.MemoryInfo,使用其availMem成员变量
- 系统总内存:调用ActivityManager#getMemoryInfo()得到ActivityManager.MemoryInfo,使用其totalMem成员变量