For any one running a linux desktop/server , the amount of free memory available on the system is one of the critical parameters that deterimine the system response. We will look at some commands which give you information about memory usage
free
The free command provides the quickest way to see how much memory is being used on your system. It shows the total amount of RAM and swap space, along
with the amount currently being used.
Here are some examples of the free command:
$ free
total used free shared buffers cached
Mem: 2051656 1333508 718148 0 157452 461856
-/+ buffers/cache: 714200 1337456
Swap: 4416664 0 4416664
free -m List memory usage in megabytes
$ free -m
total used free shared buffers cached
Mem: 2003 1302 701 0 153 451
-/+ buffers/cache: 697 1306
Swap: 4313 0 4313
free -b List memory usage in blocks
sunil@Jaunty:~$ free -b
total used free shared buffers cached
Mem: 2100895744 1366093824 734801920 0 161419264 473022464
-/+ buffers/cache: 731652096 1369243648
Swap: 4522663936 0 4522663936
free -mt List memory usage with totals displayed (Swap + Mem)
sunil@Jaunty:~$ free -mt
total used free shared buffers cached
Mem: 2003 1302 700 0 154 451
-/+ buffers/cache: 697 1305
Swap: 4313 0 4313
Total: 6316 1302 5013
To avoid wasting RAM and speed up applications, Linux uses as much otherwise unused RAM as possible for the disc cache. For that reason, the first line of output from free that often shows little free RAM can be misleading. Please pay closer attention to the second line of output, which shows the amount of RAM actually available for applications. That amount is 1305 MB in the above example which shows
-/+ buffers/cache: 697 1305
top
The top command provides a means of watching the currently running processes, along with their memory usage.
$ top
top - 10:53:00 up 29 min, 2 users, load average: 0.42, 0.46, 0.45
Tasks: 199 total, 3 running, 196 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.9%us, 2.6%sy, 0.0%ni, 91.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 2051656k total, 1335604k used, 716052k free, 158124k buffers
Swap: 4416664k total, 0k used, 4416664k free, 462016k cached
To exit top, press q. Like the output for free, top shows total of memory usage for
RAM and swap space. However, because top is screen oriented and
provides ongoing monitoring, you can watch memory usage change every three seconds (by default).
With top running, press Shift+m and the running processes will be
displayed in memory-use order (so you can watch which processes are consuming themost memory). See the screen shot above.
The most useful column to analyze a process’ memory usage is RES, which shows the process’ actual physical RAM usage, also known as resident size.
The MEM column is based on this resident size. You can look at the man page of top for more information.
vmstat
This command gives you virtual memory statistics.
sunil@Jaunty:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 682412 158804 489856 0 0 69 11 203 679 6 2 88 3
You can view memory use over a given time period. The following out put gives you vmstat every 5 seconds.
sunil@Jaunty:~$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 682856 159000 489892 0 0 67 11 204 680 6 2 89 3
1 0 0 682832 159004 489888 0 0 1 1 792 1969 4 2 94 0
0 0 0 682684 159012 489892 0 0 0 2 752 1249 4 2 94 0
0 0 0 682792 159012 489892 0 0 0 0 836 1839 4 2 94 0
The out put is printed every 5 seconds. To exit vmstat, press Ctrl+c. T$ vmstat -S m Display output in 1000k megabytes. You can try loading several applications and watch how the vmstat output changes .
The kernel itself, however, has its own memory cache to keep track of its resources, called the kernel slab. You can use the vmstat command to display kernel slab memory cache statistics (from /proc/slabinfo) as follows:
sunil@Jaunty:~$ vmstat -m
Cache Num Total Size Pages
RAWv6 23 23 704 23
TCPv6 96 96 1344 24
flow_cache 0 0 80 51
kcopyd_job 0 0 264 31
dm_uevent 0 0 2464 13
kmalloc_dma-512 16 16 512 16
mqueue_inode_cache 28 28 576 28
fuse_request 62 63 376 21
-------- --- --------
kmalloc-16 3772 4096 16 256
kmalloc-8 6093 6144 8 512
kmalloc-192 9739 10038 192 21
kmalloc-96 971 1008 96 42
--- ----- ----
...
The slab memory cache information shows each cache name, the number of objects active for that cache type, the total number of objects available for that cache type, the size of the cache (in bytes), and the number of pages for each cache.
slabtop
You can also use slabtop , similar to top to display the kernel slab info.
The slabtop output updates every three seconds. By default, slab caches are sorted by the number of objects (first column) in each cache. By pressing c you can sort by cache size instead.