android 内存监控 procrank vss pss

本文介绍如何使用VSS、RSS、PSS及USS等指标衡量Linux进程内存使用情况,并通过procrank工具展示实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过 pss 和 uss 来查看进程使用的内存情况
在模拟器上 adb shell procrank 可以查看到以下信息
zhengs-MacBook-Air:~ yajun0601$ adb shell
shell@android:/ $ procrank -p                                                 
warning: could not read usage for 2698
  PID      Vss      Rss      Pss      Uss  cmdline
 2016   78832K   64164K   44719K   43628K  system_server
 3801   61348K   61208K   39134K   37648K  android.process.acore
 2213  190408K   48296K   29257K   28352K  com.cyanogenmod.trebuchet
 2113   74216K   44292K   25702K   24816K  com.android.systemui
 2179   38532K   38420K   20892K   20032K  com.sohu.inputmethod.sogou
 2199   38148K   35016K   17414K   16740K  com.android.phone
25599   57616K   39628K   16420K   14680K  com.zdworks.android.zdclock:zdclock
24931   29920K   29840K   13524K   12964K  com.wali.NetworkAssistant
 3447   34072K   33988K   12113K   11024K  com.wandoujia.phoen
下面解释这四项:
Overview
The aim of this post is to provide information that will assist in interpreting memory reports from various tools so the true memory usage for Linux processes and the system can be determined.
Android has a tool called procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order from highest to lowest usage. The sizes reported per process are VSS, RSS, PSS, and USS.

For the sake of simplicity in this description, memory will be expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 byte pages at the lowest level.


VSS (reported as VSZ from ps) is the total accessible address space of a process. This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.


RSS is the total memory actually held in RAM for a process. RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.


PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.


USS is the total private memory for a process, i.e. that memory that is completely unique to that process. USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.

For systems that have Python available, there is also a nice tool called smem that will report memory statistics including all of these categories.

### Android 内存监控工具推荐 #### 一、Android Studio 自带的 Memory Profiler Memory Profiler 是一款强大的内置工具,能够帮助开发者实时查看应用程序的内存使用情况。该工具可以捕获堆转储并跟踪内存分配,有助于识别潜在的内存泄漏问题[^2]。 对于可能存在内存泄漏的情况,比如频繁创建对象而未释放资源的操作,可以通过多次触发这些操作来观察 MEMORY 轨迹的趋势变化;当发现内存持续增长而不下降时,则表明存在内存泄漏的风险。此外,还可以利用此功能深入探究具体哪些部分导致了异常的增长。 ```bash # 打开 Android Studio 的 Terminal 并输入命令启动应用后获取其 PID adb shell pidof -s com.example.yourapp ``` 接着,在 Android Studio 中选择对应的进程来进行详细的性能剖析。 #### 二、Procrank 和 Dumpsys Meminfo 命令行工具组合 这两个 Linux 下常用的命令可以帮助快速了解单个应用程序在整个设备中的相对内存消耗状况以及更加详尽的信息: - **Procrank**: 可以用来查询特定包名的应用程序当前所占有的物理 RAM 数量(RSS),并且支持正则表达式的过滤方式以便于精准定位目标服务或组件。 ```bash adb shell procrank | grep "com\.example" ``` - **Dumpsys Meminfo**: 提供更为全面的数据统计,包括但不限于 VSS (Virtual Set Size), RSS, PSS (Proportional Set Size) 和 USS (Unique Set Size),从而让用户更好地理解不同层次上的实际影响范围。 ```bash adb shell dumpsys meminfo com.example.anotherapp ``` 通常情况下,上述两个指标之间的关系遵循着 `VSS >= RSS >= PSS >= USS` 这样的顺序排列原则[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值