Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Memory Utilization-by-User

Submitted by on February 10, 2015 – 5:13 pm

Here’s a useful one-liner (sort of) that will show you memory utilization details by user ID on Linux (RHEL, Fedora, CentOS) systems. This may come in handy when trying to figure out who is eating up all the RAM.

ps -ef | awk '{print $1}' | egrep -v "^[0-9]|UID" | sort -u | while read i ; do printf -- '-%.s' {1..30} ; echo ; for k in Size Rss Pss Shared_Clean Shared_Dirty Private_Clean Private_Dirty Referenced Anonymous Swap KernelPageSize MMUPageSize Locked; do echo -ne "${i}\t${k}\t" ; pgrep -u ${i} | while read pid ; do cat /proc/${pid}/smaps 2>/dev/null | grep "^${k}:" | awk '{ SUM += $2} END { print (SUM) }' ; done | awk '{ SUM += $1} END { print ( SUM/1024 )" MB" }' ; done | column -t ; done

Sample output:
------------------------------
dbus  Size            20.9062   MB
dbus  Rss             0.832031  MB
dbus  Pss             0.498047  MB
dbus  Shared_Clean    0.335938  MB
dbus  Shared_Dirty    0         MB
dbus  Private_Clean   0.230469  MB
dbus  Private_Dirty   0.265625  MB
dbus  Referenced      0.703125  MB
dbus  Anonymous       0.265625  MB
dbus  Swap            0         MB
dbus  KernelPageSize  0.199219  MB
dbus  MMUPageSize     0.199219  MB
dbus  Locked          0         MB
------------------------------
ganglia  Size            142.016    MB
ganglia  Rss             1.54297    MB
ganglia  Pss             1.08203    MB
ganglia  Shared_Clean    0.460938   MB
ganglia  Shared_Dirty    0.0078125  MB
ganglia  Private_Clean   0.203125   MB
ganglia  Private_Dirty   0.871094   MB
ganglia  Referenced      0.871094   MB
ganglia  Anonymous       0.871094   MB
ganglia  Swap            0          MB
ganglia  KernelPageSize  0.398438   MB
ganglia  MMUPageSize     0.398438   MB
ganglia  Locked          0          MB
------------------------------
hbase  Size            16796.4   MB
hbase  Rss             12562.3   MB
hbase  Pss             12479.4   MB
hbase  Shared_Clean    90.2305   MB
hbase  Shared_Dirty    0.273438  MB
hbase  Private_Clean   0.046875  MB
hbase  Private_Dirty   12470.8   MB
hbase  Referenced      12295.9   MB
hbase  Anonymous       12471.1   MB
hbase  Swap            0         MB
hbase  KernelPageSize  8.07422   MB
hbase  MMUPageSize     8.07422   MB
hbase  Locked          0         MB
------------------------------
hdfs  Size            2039.29    MB
hdfs  Rss             1232.5     MB
hdfs  Pss             1214.63    MB
hdfs  Shared_Clean    19.0039    MB
hdfs  Shared_Dirty    0.0507812  MB
hdfs  Private_Clean   0          MB
hdfs  Private_Dirty   1213.45    MB
hdfs  Referenced      1218.24    MB
hdfs  Anonymous       1213.41    MB
hdfs  Swap            0          MB
hdfs  KernelPageSize  3.79688    MB
hdfs  MMUPageSize     3.79688    MB
hdfs  Locked          0          MB
------------------------------
mapred  Size            16288.6  MB
mapred  Rss             4212.4   MB
mapred  Pss             4011.21  MB
mapred  Shared_Clean    233.785  MB
mapred  Shared_Dirty    6.75781  MB
mapred  Private_Clean   2.51562  MB
mapred  Private_Dirty   4073.71  MB
mapred  Referenced      4211.5   MB
mapred  Anonymous       4092.38  MB
mapred  Swap            0        MB
mapred  KernelPageSize  19.3438  MB
mapred  MMUPageSize     19.3438  MB
mapred  Locked          0        MB
------------------------------
root  Size            7520.15  MB
root  Rss             343.648  MB
root  Pss             274.103  MB
root  Shared_Clean    67.3281  MB
root  Shared_Dirty    13.8555  MB
root  Private_Clean   4.05469  MB
root  Private_Dirty   258.473  MB
root  Referenced      266.039  MB
root  Anonymous       272.078  MB
root  Swap            0        MB
root  KernelPageSize  21.4883  MB
root  MMUPageSize     21.4883  MB
root  Locked          0        MB
------------------------------
solr  Size            80781.1    MB
solr  Rss             68151.5    MB
solr  Pss             68141      MB
solr  Shared_Clean    11.4023    MB
solr  Shared_Dirty    0.0078125  MB
solr  Private_Clean   0.5625     MB
solr  Private_Dirty   68139.5    MB
solr  Referenced      67012.1    MB
solr  Anonymous       68139.5    MB
solr  Swap            0          MB
solr  KernelPageSize  5.15234    MB
solr  MMUPageSize     5.15234    MB
solr  Locked          0          MB

Here’s a version of the same command that will show memory utilization only for specified usernames:
for i in apache tomcat weblogic ; do if [ `ps -ef | awk '{print $1}' | egrep -v "^[0-9]|UID" | sort -u | grep -xc "${i}"` -gt 0 ] ; then printf -- '-%.s' {1..30} ; echo ; for k in Size Rss Pss Shared_Clean Shared_Dirty Private_Clean Private_Dirty Referenced Anonymous Swap KernelPageSize MMUPageSize Locked; do echo -ne "${i}\t${k}\t" ; pgrep -u ${i} | while read pid ; do cat /proc/${pid}/smaps 2>/dev/null | grep "^${k}:" | awk '{ SUM += $2} END { print (SUM) }' ; done | awk '{ SUM += $1} END { print ( SUM/1024 )" MB" }' ; done | column -t ; fi ; done

And a similar example, but using the “/proc/${pid}/status” file:
for i in apache tomcat weblogic ; do if [ `ps -ef | awk '{print $1}' | egrep -v "^[0-9]|UID" | sort -u | grep -xc "${i}"` -gt 0 ] ; then printf -- '-%.s' {1..30} ; echo ; for k in VmPeak VmSize VmLck VmHWM VmRSS VmData VmStk VmExe VmLib VmPTE VmSwap; do echo -ne "${i}\t${k}\t" ; pgrep -u ${i} | while read pid ; do cat /proc/${pid}/status 2>/dev/null | grep "^${k}:" | awk '{ SUM += $2} END { print (SUM) }' ; done | awk '{ SUM += $1} END { print ( SUM/1024 )" MB" }' ; done | column -t ; fi ; done

 

Print Friendly, PDF & Email

Leave a Reply