How to Calculate Memory Usage in Linux using sar, ps, and free

It is important to find the process or application hogging memory of the system in case of a performance issue. The post lists few of the basic commands to calculate memory usage on a general Linux system.

1. Calculate memory usage using SAR

# sar -r 1 10
Linux 2.6.32-504.el6.x86_64 (geeklab)  03/05/2018  _x86_64_ (16 CPU)

10:17:41 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit
10:17:42 AM   1772488  31104712     94.61    382184  23831756  11675188     11.86
10:17:43 AM   1777348  31099852     94.59    382184  23831756  11675188     11.86
10:17:44 AM   1778412  31098788     94.59    382184  23831756  11675188     11.86
10:17:45 AM   1776720  31100480     94.60    382184  23831756  11675072     11.86
10:17:46 AM   1777932  31099268     94.59    382184  23831760  11675072     11.86
10:17:47 AM   1780848  31096352     94.58    382184  23831760  11675072     11.86
10:17:48 AM   1774460  31102740     94.60    382184  23831760  11675644     11.86
10:17:49 AM   1740080  31137120     94.71    382184  23831760  11717568     11.91
10:17:50 AM   1773608  31103592     94.61    382184  23831708  11675076     11.86
10:17:51 AM   1774752  31102448     94.60    382184  23831796  11675076     11.86
Average:      1772665  31104535     94.61    382184  23831757  11679414     11.87

To calculate free memory from Average value use the below formula:

kbmemfree + kbbuffers + kbcached = actual free memory on the system

Using above example:

1772665 + 382184 + 23831757 = 25986606KB

That’s around 24.78GB free memory.

2. Calculate memory usage using free command

# free -m
             total       used       free     shared    buffers     cached
Mem:         32106      30382       1723       3031        373      23273
-/+ buffers/cache:       6736      25370
Swap:        63999          2      63997

From above example, to get free memory on the system use:

-/+ buffers/cache ==> free column

Free column reports 25370 MB so that match sar output calculation as well ( keeping in mind that in SAR Average value was used )

To get used memory on system use:

-/+ buffers/cache ==> used column

In our case the used memory is 6736MB.

3. Find Top 10 users are which are consuming more memory on system in percentage

Please use simple shell syntax which utilize ps command:

# ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END{printf("NPROC\tUSER\tCPU\tMEM\n"); for (user in cpu) printf("%d\t%s\t%.2f\t%.2f\n",num[user], user, cpu[user], mem[user]) }'
NPROC USER   CPU MEM
29 oracle   0.10 4.00
1 ntp   0.00 0.00
1 rpc   0.00 0.00
5 user   0.00 0.00
1 dbus   0.00 0.00
1 mysql   0.00 0.30
12 daemon   0.00 0.60
2 postfix   0.00 0.00
12 apache1   2.40 4.80
1 rpcuser   0.00 0.00
349 root   7.70 3.30

From the above example observation, root has 349 processes which are taking 3.3% of memory, user oracle has 29 processes which are taking 4% of memory, user apache1 has 12 processes consuming around 4.8% of memory, that in total around 12%. Around 12% value can be also seen on sar ‘commit’ column.

4. Check 10 Top processes which are consuming RSS ( Resident Set Size )

Please use simple shell syntax which utilize ps command:

# ps -e -orss,pid=,user=,args=, | sort -b -k1,1n | pr -TW$COLUMNS| tail -10
74336   5509 root     /opt/perf/bin/perfalarm
77212   8389 oracle   /grid/CRS/bin/ohasd.bin reboot
78216   9731 root     /opt/OV/lbin/agtrep/agtrep -start
96768   9808 root     /opt/OV/hpcs/hpsensor
108580 12073 root     /usr/lib/systemd/systemd-journald
136260 76477 root     storapid start -name storapid
178316  5350 root     /opt/OV/bin/oacore oacore /var/opt/OV/conf/oa/PipeDefinitions/oacore.xml
186168 10367 oracle   /grid/CRS/bin/oraagent.bin
1013484 31562 root    vxconfigd -x syslog
1317360 76463 root    vxencryptd

First column is RSS, second one is PID, the third one is USER and fourth is Command Executed.

RSS value is the most important one here as this shows how much memory the process has actually allocated in KB, please do not use VSZ value which calculates ‘requested’ memory by process, as each process is allocating ‘overhead’ which is presented by VSZ value, where RSS reports true memory allocation by process.

After RSS output calculation, the ‘used‘ memory can be obtained and compared with -/+ buffers/cache – used column from the output of free command — both RSS and the output from the free command should match.

Related Post