August 9, 2011

Linux using my RAM

Q. How do I find out what process are eating up all my memory. Is it possible to find out how long that memory has been allocated to particular process? How do I kill that process to free up memory?

A. You need to use the top command which provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.
Simply type top command:

# top

top command will tell you the percentage of physical memory a particular process is using at any given time. As far as I know, there is no easy way that can tell how long that memory has been allocated.
You can also use ps command to get more information about process.

# ps aux | less

To kill process uses kill command

'free' and /proc
The 'free' command shows the memory on a machine, in certain categories.
[need explanation of categories here...'man free' doesn't explain the numbers]

$ free
total used free shared buffers cached
Mem: 507564 481560 26004 0 68888 185220
-/+ buffers/cache: 227452 280112
Swap: 2136604 105168 2031436

This information is obtained from /proc/meminfo, which has additional details not shown by the 'free' command.
The following is on my machine with 512 Mb RAM, running Linux 2.6.3:

$ cat /proc/meminfo

MemTotal: 507564 kB
MemFree: 26004 kB
Buffers: 68888 kB
Cached: 185220 kB
SwapCached: 29348 kB
Active: 342488 kB
Inactive: 32092 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 507564 kB
LowFree: 26004 kB
SwapTotal: 2136604 kB
SwapFree: 2031436 kB
Dirty: 88 kB
Writeback: 0 kB
Mapped: 165648 kB
Slab: 73212 kB
Committed_AS: 343172 kB
PageTables: 2644 kB
VmallocTotal: 524212 kB
VmallocUsed: 7692 kB
VmallocChunk: 516328 kB


meminfo:

Provides information about distribution and utilization of memory. This
varies by architecture and compile options. The following is from a
16GB PIII, which has highmem enabled. You may not have all of these fields.

> cat /proc/meminfo

MemTotal: 16344972 kB
MemFree: 13634064 kB
Buffers: 3656 kB
Cached: 1195708 kB
SwapCached: 0 kB
Active: 891636 kB
Inactive: 1077224 kB
HighTotal: 15597528 kB
HighFree: 13629632 kB
LowTotal: 747444 kB
LowFree: 4432 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 968 kB
Writeback: 0 kB
Mapped: 280372 kB
Slab: 684068 kB
Committed_AS: 1576424 kB
PageTables: 24448 kB
ReverseMaps: 1080904
VmallocTotal: 112216 kB
VmallocUsed: 428 kB
VmallocChunk: 111088 kB

MemTotal: Total usable ram (i.e. physical ram minus a few reserved
bits and the kernel binary code)
MemFree: The sum of LowFree+HighFree
Buffers: Relatively temporary storage for raw disk blocks
shouldn't get tremendously large (20MB or so)
Cached: in-memory cache for files read from the disk (the
pagecache). Doesn't include SwapCached
SwapCached: Memory that once was swapped out, is swapped back in but
still also is in the swapfile (if memory is needed it
doesn't need to be swapped out AGAIN because it is already
in the swapfile. This saves I/O)
Active: Memory that has been used more recently and usually not
reclaimed unless absolutely necessary.
Inactive: Memory which has been less recently used. It is more
eligible to be reclaimed for other purposes
HighTotal:
HighFree: Highmem is all memory above ~860MB of physical memory
Highmem areas are for use by userspace programs, or
for the pagecache. The kernel must use tricks to access
this memory, making it slower to access than lowmem.
LowTotal:
LowFree: Lowmem is memory which can be used for everything that
highmem can be used for, but it is also availble for the
kernel's use for its own data structures. Among many
other things, it is where everything from the Slab is
allocated. Bad things happen when you're out of lowmem.
SwapTotal: total amount of swap space available
SwapFree: Memory which has been evicted from RAM, and is temporarily
on the disk
Dirty: Memory which is waiting to get written back to the disk
Writeback: Memory which is actively being written back to the disk
Mapped: files which have been mmaped, such as libraries
Slab: in-kernel data structures cache
Committed_AS: An estimate of how much RAM you would need to make a
99.99% guarantee that there never is OOM (out of memory)
for this workload. Normally the kernel will overcommit
memory. That means, say you do a 1GB malloc, nothing
happens, really. Only when you start USING that malloc
memory you will get real memory on demand, and just as
much as you use. So you sort of take a mortgage and hope
the bank doesn't go bust. Other cases might include when
you mmap a file that's shared only when you write to it
and you get a private copy of that data. While it normally
is shared between processes. The Committed_AS is a
guesstimate of how much RAM/swap you would need
worst-case.
PageTables: amount of memory dedicated to the lowest level of page
tables.
ReverseMaps: number of reverse mappings performed
VmallocTotal: total size of vmalloc memory area
VmallocUsed: amount of vmalloc area which is used
VmallocChunk: largest contigious block of vmalloc area which is free


This command will list all of your processes sorted by memory usage:

ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r | more

The first column shows the percentage of memory used by the process. You can use this information to find out which process is using the most.


ps -A --sort -rss -o comm,pmem | head -n 11

ps -A --sort -rss -o pid,comm,pmem,rss

This will give you the 10 processes using the most ram

lsb_release -a && free -m

From this command you get information of version of linux , ram,and memory status information.




Use a tool called pmap. It reports the memory map of a process or processes.
pmap examples

To display process mappings, type
$ pmap pid
$ pmap 3724
Output:
3724: /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
0000000000400000 164K r-x-- /usr/sbin/lighttpd
0000000000629000 12K rw--- /usr/sbin/lighttpd
000000000bb6b000 4240K rw--- [ anon ]
00000035ee600000 104K r-x-- /lib64/ld-2.5.so
00000035ee819000 4K r---- /lib64/ld-2.5.so
00000035ee81a000 4K rw--- /lib64/ld-2.5.so
00000035eea00000 1304K r-x-- /lib64/libc-2.5.so
00000035eeb46000 2048K ----- /lib64/libc-2.5.so
00000035eed46000 16K r---- /lib64/libc-2.5.so
00000035eed4a000 4K rw--- /lib64/libc-2.5.so
00000035eed4b000 20K rw--- [ anon ]
00000035eee00000 8K r-x-- /lib64/libdl-2.5.so
00000035eee02000 2048K ----- /lib64/libdl-2.5.so
.....
....
00002aaaac51e000 4K r---- /lib64/libnss_files-2.5.so
00002aaaac51f000 4K rw--- /lib64/libnss_files-2.5.so
00007fff7143b000 84K rw--- [ stack ]
ffffffffff600000 8192K ----- [ anon ]
total 75180K
The -x option can be used to provide information about the memory allocation and mapping types per mapping. The amount of resident, non-shared anonymous, and locked memory is shown for each mapping:
pmap -x 3526


Clearing the disk cache

For experimentation, it's very convenient to be able to drop the disk cache. For this, we can use the special file /proc/sys/vm/drop_caches. By writing 3 to it, we can clear most of the disk cache:
$ free -m
total used free shared buffers cached
Mem: 1504 1471 33 0 36 801
-/+ buffers/cache: 633 871
Swap: 2047 6 2041

$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ free -m
total used free shared buffers cached
Mem: 1504 763 741 0 0 134
-/+ buffers/cache: 629 875
Swap: 2047 6 2041

Notice how "buffers" and "cached" went down, free mem went up, and free+buffers/cache stayed the same.


You can refer this link for additional information.