Sonntag, 16. Dezember 2007

Identify Programs consuming much Memory from command line

I'll use two commands here: "free" and "ps".

Because my Laptop own's only 512 MB RAM and has a terrible slow hard disk drive, I try to optimize my memory. (the more RAM Cache i have, the better!)

To see, how much memory is currently used:

$ free -m
total used free shared buffers cached
Mem: 503 492 10 0 11 196
-/+ buffers/cache: 285 218
Swap: 1004 134 869



Short interpretation of there lines:
- the "-m" switch: use unit "MBytes"
- i have 512 MB RAM (max availbale is 503 MB, the 9 MB difference is eaten by the linux kernel)
- My applications currently need 285 MB physical RAM and 134 MB SWAP
- 218 MB physical RAM is not used by applications, but used from Linux to spped up my sy for disk cache - 285 MB + 11 MB + 196 MB = 492 MB (used physical RAM)
- 10 MB are "wasted" (not currently used). This may be memory which was freed by an application, which i closed a few seconds ago.


Now, which applications are using the 285 MB physical Memory?? (see http://m0han.blogspot.com/2007/05/howto-profile-memory-in-linux-system.html)


$ ps -eo user,pid,rss,vsize,pcpu,pmem,cmd -ww --sort=rss
USER PID RSS VSZ %CPU %MEM CMD
[...]
jolzer 5659 10844 34016 0.0 2.1 kdesktop [kdeinit]
jolzer 5682 12224 56964 1.4 2.3 psi -session 10145140d8a1000119770190900000056460050_1197759181_634572
jolzer 5694 12224 34604 0.0 2.3 konsole [kdeinit] -session 10145140d8a10001197749424000000
jolzer 5646 12692 54752 0.1 2.4 kded [kdeinit] --new-startup
jolzer 5661 15616 43964 0.1 3.0 kicker [kdeinit]
jolzer 5693 19940 57608 0.2 3.8 krusader -session 10145140d8a1000119773080200000056060020_1197759181_635552
root 5097 69672 88644 1.8 13.5 /usr/bin/X -br -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-TRonOl
jolzer 5688 103524 187552 0.5 20.0 digikam -session 10145140d8a1000119772494000000056060017_1197759181_635294
jolzer 12315 114272 295228 12.0 22.1 /usr/lib/firefox-3.0/firefox-3.0


Most interesting here is the third column: "RSS" ("Resident Set Size") - This shows in KB, how much physical RAM is needed by the process. (swapped out Memory is not counted)

Second most interesting is the fourth column: "VSZ" = "VSIZE" = "Virtual memorysize" = Memory inclusive "swapped out memory".

So you see, i'm using "Firefox Gran Paradiso" (3.0 beta) at this time, which is taking 114MB of my physical memory. By the way, to reduce the memory firefox need, thy search the internet for "firefox reduce memory" - here two good links:
- http://www.firefox-browser.de/wiki/FAQ:Speicherbedarf (german)
- http://kb.mozillazine.org/Reducing_memory_usage_-_Firefox (english)

I've created a small shell script:
(filename "~/bin/jzps")

#!/bin/bash
ps -eo user,pid,rss,vsize,pcpu,pmem,cmd -ww --sort=rss
echo "-- "
free -m


I use this command everytime, my linux gets slower and so i'll learn, which applications eat up too much memory.

But: Don't trust these numbers!!!

Reason: Applications also use "shared memory", which may by used by more than one process.

Workaround: To see, how much memory is really used, use, "free -m" before and after starting the application. This value is more accurate, but difficult to handle: if you are observice memory usage of firefox while surfing, you (and your system!) must not start any other programs.... If you quit firefox, "free -m" should show the same amount of free memory like it showed first.

Keine Kommentare: