Linux Top Command - Find How Much Memory The Process Use

September 13, 2013 | By
| Reply More

However, generally we look at the memory usage as the 'free' command that provide 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 display how much memory % a process is consuming separately for each running process. Top sort processes as cpu utilization in general, it reports an ongoing look at processor activity in real time. It displays a listing of the most CPU utilization tasks and also can sort the tasks by CPU usage, memory usage and runtime. To check memory usges for each process we press m in running top command.

Open shell to run top command, if we will run top it will display only command name of running process to see full command we use -c option with top.

top -c

top - 15:58:50 up 3 min, 2 users, load average: 0.05, 0.12, 0.06
Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3%us, 0.7%sy, 0.0%ni, 98.7%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Mem: 1031320k total, 98908k used, 932412k free, 6472k buffers
Swap: 2064376k total, 0k used, 2064376k free, 32308k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1231 root 20 0 2520 1088 876 R 0.7 0.1 0:00.18 top -c
1214 root 20 0 13168 5264 2884 S 0.3 0.5 0:00.63 sshd: root@pts/0
1 root 20 0 2824 1360 1184 S 0.0 0.1 0:03.98 /sbin/init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kthreadd]
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 [migration/0]
4 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [ksoftirqd/0]
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 [watchdog/0]
6 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [events/0]
7 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [cpuset]
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [khelper]

and after pressing m it will sort output as memory usage as below:

top - 16:01:16 up 5 min, 2 users, load average: 0.00, 0.07, 0.04
Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3124 mysql 20 0 2673 1023 774 S 1.0 2.1 0:23:34 mysql
1214 root 20 0 13168 5264 2884 S 0.3 0.5 0:00.87 sshd: root@pts/0
1231 root 20 0 2520 1092 880 R 1.0 0.1 0:00.98 top -c
1 root 20 0 2824 1360 1184 S 0.0 0.1 0:03.98 /sbin/init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kthreadd]
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 [migration/0]
4 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [ksoftirqd/0]
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 [watchdog/0]

As here mysql consuming maximum memory and then sshd and then top. There is an other menthod of top command to check memory usge for process.

Run top command and press shift+f then 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.

Press the Letter corresponding to %MEM
Press ENTER

It will also sort top output as memory usage.

Filed Under : LINUX HOWTO, MONITORING

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.