Ubuntu – How to use free -m to get memory usage for all users

12.04command linefreememory usage

I typed the free command to get the memory usage as follows :

free -m 

output:

enter image description here

I want to use this command to get the same info but for all users for example :

enter image description here

I used this command .. because it easy for me .. to store its output in a variables in a bash scrip …

Best Answer

free is already based on the system-wide memory usage.

If you want something on a per-user basis, you could try something like:

ps aux | awk 'NR>2{arr[$1]+=$6}END{for(i in arr) print i,arr[i]}'

As a quick explanation of what the awk does:

  • Strips off the first line
  • Iterates through each line and creates an array of every given username. For each of those it adds the sixth column from ps aux (resident set size) and adds them up.
  • After that it just iterates through the array keys to print the content.