Linux 进程内存使用统计VSS RSS PSS USSS

有的时候我们需要统计不同的进程实际物理内存的消耗,常见的几个参数如下:
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.

其中VSS>RSS>PSS>USS
对于RSS PSS USS的统计,可以通过代码简单的
system/extras/libpagemap/pm_map.c

usage.rss += (count >= 1) ? map->proc->ker->pagesize : (0);
usage.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
usage.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);

通过上面的算法可以看出,USS部分是进程独占的部分,PSS是USS+共享/count, RSS是USS+共享部分
如果我们要对进程退出后内存回收的统计的话,应该使用USS部分

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容