Generally, we look at the memory usage using the free command that provides us the total physical memory and used memory out of total memory. But top command is more useful to check memory usage in Linux. The free command display only physical memory usage but top display virtual memory usages by each process. It displays how much memory
% a process is consuming separately for each running process.
Top command displays a listing of the most CPU utilization tasks and also can sort the tasks by CPU usage, memory usage and runtime.
Top sort by memory
Open shell to run top command, if we run top it will display only command name of the running process, to see full command we use
-c option with top.
$ top -c
m it will sort output with an order of highest memory usage at top as below:
top - 07:12:37 up 69 days, 4:33, 1 user, load average: 0.17, 0.10, 0.06 Tasks: 164 total, 1 running, 98 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.2 us, 0.5 sy, 0.0 ni, 96.0 id, 0.0 wa, 0.2 hi, 0.2 si, 0.0 st KiB Mem : 29.3/4024444 [||||||||||||||||||||||||||||| ] KiB Swap: 9.5/524284 [|||||||||| ] PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 24426 www-data 20 0 449796 62568 45484 S 5.3 1.6 0:25.12 php-fpm: pool www 19577 www-data 20 0 448112 60288 44652 S 1.7 1.5 0:29.16 php-fpm: pool www 1233 root 20 0 1292628 41960 5188 S 0.3 1.0 580:00.20 /opt/site24x7/monagent/lib/Site24x7Agent 1928 root 20 0 1183840 43808 5368 S 0.3 1.1 56:50.99 /opt/site24x7/monagent/lib/applog/Site24x7Applog 7859 root 20 0 41856 3880 3200 R 0.3 0.1 0:00.07 top -c 1 root 20 0 37672 4604 3240 S 0.0 0.1 14:35.54 /sbin/init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.78 [kthreadd] 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 [rcu_gp] 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 [rcu_par_gp]
Also you can run top command as following
m from the keyboard to sort by memory usage.
Using shortcut keys
Modern Linux distro supports interactive menu for top to select memory usage.
Follow the below steps:
Shift+fto enter the interactive menu.
- press the
downarrow until the
%MEMchoice is highlighted.
enterto save your selection and exit (
q)the interactive menu.
Below screenshot shows the interactive menu
In older version of Linux it would be bit different as follows:
top command and press
f then the output will as displayed as shown below:
Current Sort Field: K for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id b: PPID = Parent Process Pid Note2: c: RUSER = Real user name Field sorting uses internal values, d: UID = User Id not those in column display. Thus, e: USER = User Name the TTY & WCHAN fields will violate f: GROUP = Group Name strict ASCII collating sequence. g: TTY = Controlling Tty (shame on you if WCHAN is chosen) h: PR = Priority i: NI = Nice value j: P = Last used cpu (SMP) * K: %CPU = CPU usage l: TIME = CPU Time m: TIME+ = CPU Time, hundredths n: %MEM = Memory usage (RES) o: VIRT = Virtual Image (kb) p: SWAP = Swapped size (kb) q: RES = Resident size (kb) r: CODE = Code size (kb) s: DATA = Data+Stack size (kb) t: SHR = Shared Mem size (kb) u: nFLT = Page Fault count v: nDRT = Dirty Pages count w: S = Process Status x: COMMAND = Command name/line y: WCHAN = Sleeping in Function z: Flags = Task Flags
Now press the letter corresponding to
%MEM ie 'n' and then press 'ENTER' key.
Using a single command
You can run the following single top command from the terminal to display memory usage:
$ top -o %MEM
If you have any questions or feedback, feel free to leave a comment.