Posts

How to Delete Buffers and Cache Memory in Linux

Step 1: Check Buffers & Cache Memory

To check the buffers and cache memory in linux, execute the following command:

# free
              total        used        free      shared  buff/cache   available
Mem:        1015944      211640       94724       57700      709580      562416
Swap:             0           0           0

Step 2: Check Buffers & Cache Memory

Now delete and check the buffers and cache memory it will be reduced:

# sync && echo 3 > /proc/sys/vm/drop_caches && free

Following are the Linux Kernal that how can we drop the various aspects of cached items by changing the numeric argument to the above command
To free pagecache:

# echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

# echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

# echo 3 > /proc/sys/vm/drop_caches

How to Check Memory in Linux

Following are some useful commands to check the memory status of machine:

free Command

Use free command to check the memory, buffers, cache memory

# free -m
             total       used       free     shared    buffers     cached
Mem:          6082       5801        280          0        155       3887
-/+ buffers/cache:       1759       4322
Swap:         1707         33       1674

top Command

Top command tells the detailed information of CPU i.e time, load average, CPU utilization, memory, swap memory.

# top
top - 09:36:16 up 33 days, 16:47,  2 users,  load average: 0.49, 0.47, 0.52
Tasks: 157 total,   1 running, 155 sleeping,   1 stopped,   0 zombie
Cpu(s): 30.0%us,  2.4%sy,  0.0%ni, 67.0%id,  0.5%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:   6228500k total,  5975956k used,   252544k free,   159264k buffers
Swap:  1748984k total,    34100k used,  1714884k free,  3980880k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11774 www-data  20   0 99.8m  56m 6780 S   55  0.9   1:02.52 apache2
21086 www-data  20   0  100m  54m 5620 S   39  0.9   0:04.14 apache2
21075 www-data  20   0 66524  19m 4848 S    4  0.3   0:21.04 apache2

htop Command

Install htop on server, htop command also tells the detailed information of CPU i.e time, load average, CPU utilization, memory, swap memory but in a nice format.

# htop

Check Memory Consumption Processes

Process that are eating more memory

# ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20