Android系统中提供了两个命令行工具procrank、procmem用于查看系统中的内存使用情况。procrank可以查看系统中所有进程的整体内存占用情况,并按照规则排序。而procmem可以针对某个特定的进程分析其堆、栈、共享库等内存占用情况。这两个工具对于我们分析内存相关问题非常有效。由于Android系统使用的是Linux内核,理论上这样的工具可以在Linux上运行。
编译
参考Android系统中procrank、procmem的Android.mk发现他们依赖于libpagemap库,需要将其一并移植到Linux系统中。
Makefile可以通过CMake自动生成,当然前提是需要安装cmake工具。
sudo apt-get install cmake
参考Android.mk,可以写出如下的CMakeLists.txt.
cmake_minimum_required(VERSION 2.8)
PROJECT (mem_proc)
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/libpagemap/*.c")
add_definitions (
-D_LARGEFILE64_SOURCE
)
include_directories (${PROJECT_SOURCE_DIR}/libpagemap/include)
add_library(pagemap ${SOURCES})
add_executable(procmem ${PROJECT_SOURCE_DIR}/procmem/procmem.c)
target_link_libraries(procmem pagemap)
add_executable(procrank ${PROJECT_SOURCE_DIR}/procrank/procrank.c)
target_link_libraries(procrank pagemap)
编译过程如下:
-从GitHub下载源码
david-virtual-machine% git clone https://github.com/sunao2002002/mem_proc.git
正克隆到 'mem_proc'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 22 (delta 4), reused 22 (delta 4), pack-reused 0
展开对象中: 100% (22/22), 完成.
检查连接... 完成。
-运行cmake生成Makefile
david-virtual-machine% cd mem_proc
david-virtual-machine% ls
build.sh CMakeLists.txt libpagemap procmem procrank
david-virtual-machine% mkdir out
david-virtual-machine% cd out
david-virtual-machine% cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/david/github/mem_proc/out
david-virtual-machine% ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
david-virtual-machine%
-make生成可执行文件
david-virtual-machine% make
Scanning dependencies of target pagemap
[ 11%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_memusage.c.o
[ 22%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_process.c.o
[ 33%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_map.c.o
[ 44%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_kernel.c.o
[ 55%] Linking C static library libpagemap.a
[ 55%] Built target pagemap
Scanning dependencies of target procmem
[ 66%] Building C object CMakeFiles/procmem.dir/procmem/procmem.c.o
[ 77%] Linking C executable procmem
[ 77%] Built target procmem
Scanning dependencies of target procrank
[ 88%] Building C object CMakeFiles/procrank.dir/procrank/procrank.c.o
/home/david/github/mem_proc/procrank/procrank.c: In function ‘getprocname’:
/home/david/github/mem_proc/procrank/procrank.c:381:9: warning: implicit declaration of function ‘asprintf’ [-Wimplicit-function-declaration]
if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
^
/home/david/github/mem_proc/procrank/procrank.c:408:56: warning: comparison between pointer and integer
if (strncpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
^
[100%] Linking C executable procrank
[100%] Built target procrank
david-virtual-machine% file procrank procmem
procrank: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=19cdcb1d4674c66a3ea62e3bc026f01773a43ce0, not stripped
procmem: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=20e20cce4fd40bc859eca4d91d298f4d5165c8ff, not stripped
david-virtual-machine%
-也可以直接运行build.sh完成整个流程。
运行
procrank
procrank是按照内存占用情况对进程进行排序。因为它需要遍历/proc下的所有进程获取内存占用情况,所以在运行时候需要有root权限。可用排序的有VSS、RSS、PSS、USS。
VSS:Virtual Set Size,虚拟内存耗用内存,包括共享库的内存
RSS:Resident Set Size,实际使用物理内存,包括共享库
PSS:Proportional Set Size,实际使用的物理内存,共享库按比例分配
USS:Unique Set Size,进程独占的物理内存,不计算共享库,也可以理解为将进程杀死能释放出的内存
一般VSS >= RSS >= PSS >= USS
procrank默认是按照PSS降序排列
david-virtual-machine% sudo ./procrank -h
Usage: ./procrank [ -W ] [ -v | -r | -p | -u | -s | -h ]
-v Sort by VSS.
-r Sort by RSS.
-p Sort by PSS.
-u Sort by USS.
-s Sort by swap.
(Default sort order is PSS.)
-R Reverse sort order (default is descending).
-c Only show cached (storage backed) pages
-C Only show non-cached (ram/swap backed) pages
-k Only show pages collapsed by KSM
-w Display statistics for working set only.
-W Reset working set of all processes.
-h Display this help screen.
david-virtual-machine%
运行如下截图
procmem
procrank 给出了系统整体的内存占用情况,针对每一个进程具体分析就要使用procmem工具了。同样运行procmem需要root权限。
以上图1793号进程 /bin/sh为例,分析内存占用情况:
procmem 给出了procrank中VSS、RSS、PSS、USS的每一部分组成,包括进程可执行程序本身、共享库、堆、栈的内存占用。这里的信息应该是从/proc/pid/maps文件中获取的.