How to Check Memory Usage of Process with Linux Top Command

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. 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.

Method 1: By Pressing m key

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

Then press 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]

Method 2: Using shortcut keys

Run top command and press shift+f then the output will as 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

Note1:
If a selected sort field can't be shown due to screen width or your field order, the keys will be unavailable until a field within viewable range is chosen.

in the above example press the letter corresponding to %MEM ie "n" and then press "ENTER" key.

For newer version try below

  • press Shift+f to enter the interactive menu
  • press the up or down arrow until the %MEM choice is highlighted
  • press s to select %MEM choice
  • press enter to save your selection and exit the interactive menu

Method 3: Command Line

You can try using command line single command

# top -o %MEM

Read Also:

Bobbin Zachariah 9:15 am

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.