How to Check Memory Usage in Linux using SMEM ?

smem is a tool that can give numerous reports on memory usage on Linux systems. Unlike existing tools, smem can report proportional set size (PSS), which is a more meaningful representation of the amount of memory used by libraries and applications in a virtual memory system.

Because large portions of physical memory are typically shared among multiple applications, the standard measure of memory usage known as resident set size (RSS) will significantly overestimate memory usage. PSS instead measures each application’s “fair share” of each shared area to give a realistic measure. For more details of smem, refer to smem website

Compile SMEM for Embedded Linux Target

$ sudo apt install mercurial
$ hg clone http://selenic.com/repo/smem
$ cd smem

You can cross compile smem using cross toolchain as,

$ arm-linux-gnueabi-gcc smemcap.c -o smem-arm

You can also static cross compile to avoid any library loading errors for embedded targets as,

$ arm-linux-gnueabi-gcc smemcap.c -static -o smem-arm-static

Now, if you are testing on Embedded platform, push/copy the cross compiled binary to embedded target and run the command to capture the reports as,

$ ./smem-arm > smem_reports.tar

For Ubuntu / Desktop Linux

You can install smem using below command,

$ sudo apt-get install smem

Now, generate the smem report as,

$ smem --pie=command

here, we are saving the the collected memory report to /tmp directory, you can save it to any location.

For Android,

Since android uses different toolchain, its better to push static smem and use it to generate the smem_reports.tar and use the same to analyse. Using below command we pushed smem static to android device over adb and pulled the logs using adb, other steps to analyse the reports remains same as other embedded targets.

$ adb push smem-arm-static /sdcard 
$ adb shell
$ chmod 777 /sdcard/smem-arm-static
$ cd /sdcard
$ ./smem-arm-static > smem_reports.tar
$ exit
$ adb pull /sdcard/smem_reports.tar

For other usage of smem command, refer to smem website as mentioned earlier, in this post, we will just demo how you can create proper report using smem.

Now download this smem_reports.tar to your Linux host / PC. Install smem for host as,

$ sudo apt-get install smem

We install smem for host since it will also install the other required packages such as python-matplotlib which are required to create chart reports.

Now, you can create report as,

$ smem -S smem_reports.tar --pie=command

Reference – https://elinux.org/Using_smem_on_Android

Leave a Comment