性能监控之内存篇

RAM(Random-access memory),内存,是非常重要的资源,尤其是在手机这样的受限设备上。
不正确的使用会导致内存泄漏、内存抖动等问题,影响app性能及用户体验,因此我们需要对app内存使用状况进行监控。

那么app内存的使用状况应该怎样查看呢?

内存查看工具

先让我们使用Android Studio内置的开发工具Profiler来看一下app的内存使用状况:

Image of Profiler

我们可以看到红框标出的实时采集的内存状况。

那么这些值分别来自哪里呢?让我们再用dumpsys meminfo来看一下。

adb shell dumpsys meminfo com.jingdong.shperfmonitor
Image of dumpsys-meminfo

我们可以看到红框里的各项数值基本与之前我们使用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;

注意:

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,我们可以实现最终的监控方案:

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 几个月之前看过一遍,结果过一阵子在查bitmap造成内存泄露时又感觉忘得差不多了,不晓得android os RA...
    mrz_233333阅读 430评论 0 0
  • 内存管理机制概述 Android 的内存管理机制可以简单概括为:系统没有为内存提供交换区,它使用 paging (...
    小面包屑阅读 2,196评论 0 1
  • 自从父亲去逝后,我们家就成了“谈嫂色变”,嫂子成了我们家的母老虎,我们姊妹六个,哥姐们把嫂子当神供着,唯有我不知内...
    叶子_aba5阅读 5,514评论 42 102
  • 夏天到了,瞌睡虫王国一片沸腾,它们纷纷飞出洞口去寻找自己的朋友。 一只瞌睡虫正在想自己找谁做朋友...
    小郑童鞋阅读 4,459评论 0 7
  • 本周作业:记得带着觉知去种下智慧的种子定向回向给自己可以种下健康的种子! 早上听赵老师和李敏的分享,随喜赵老师种下...
    whj夏日心情阅读 156评论 0 2