How to Clear Linux Cache

     

    Today, I checked the memory usage of the development server with spot on light, and only 60MB was left.

You can clear the cache using the following method.
Frequent file access can cause a significant increase in system Cache usage.
First, use free -m to check the remaining memory.
[root@Oracle ~]# free -m
                                     total       used       free     shared    buffers     cached
Mem:                            3383       3319         63          0         97       2395
-/+ buffers/cache:         826         2556
Swap:                           1983       195       1788

total: Total memory  used: Memory currently in use free: Unused memory shared: Total memory shared by multiple processes

Note: It’s best to run sync before releasing memory to prevent data loss. 

Usage: sync
Description: In Linux, data intended for writing to the hard disk is sometimes, for efficiency,

     written to the filesystem buffer. This buffer is a memory space.

     If the data to be written to the disk resides in this buffer and the system suddenly loses power,

     that data will be lost. The sync command forces the data in the buffer to be written to the hard disk.

 

[root@oracle ~]# echo 1 > /proc/sys/vm/drop_caches
[root@oracle ~]# sysctl -p

[root@oracle ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3383       1952       1431          0          1       1136
-/+ buffers/cache:        814       2568
Swap:         1983        195       1788

 

Explanation:
1>. /proc is a virtual filesystem; we can use read/write operations on it as a means to communicate with the kernel entity. That is, by modifying files within /proc, we can adjust the current kernel’s behavior. In this case, we can release memory by adjusting /proc/sys/vm/drop_caches.
0 – Do not release
1 – Release page cache
2 – Release dentries and inodes
3 – Release all caches
Number 1 is used to clear the page cache for recently accessed files.
Number 2 is used to clear file node caches and directory entry caches.
Number 3 is used to clear all the caches from options 1 and 2.
2>. The official documentation for drop_caches is as follows:
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.
 
3>. The Linux kernel caches recently accessed file pages in memory for a period of time. This file cache is known as the pagecache.
An Inode is a data structure in Linux/Unix operating systems that contains important information related to each file. A large number of inodes are created when a filesystem is created. Typically, the inode table occupies 1% of the filesystem’s disk space.
 

Directory entry cache(dcache)dentries

 

Meaning of each parameter:
total: Total physical memory
used: Memory currently in use
free: Memory completely unused
shared: Memory shared by applications
buffers: Cache, mainly used for directories, inode values, etc.
cached: Cache for opened files
-buffers/cache: Memory size used by applications, calculated as used minus cache values
+buffers/cache: Total memory size available for applications, calculated as free plus cache values
 
Where:
total = used + free
-buffers/cache=used-buffers-cached, which is the actual memory size used by applications
+buffers/cache=free+buffers+cached, which is the actual memory size the server can still utilize
[oracle@cddserver1 ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:         32096      10379      21717          0         38       7942
-/+ buffers/cache:       2398      29698
Swap:        34287          0      34287
The second line here represents the actual free memory of the server.
 
 
2. How Linux Allocates Memory
As everyone knows, to improve efficiency, a Linux server pre-allocates memory. Even if this memory isn’t used by specific applications, Linux will request it in advance and then use it as cache, storing recently accessed files into the cache. This causes the server’s free value to decrease while buffers and cached values keep growing, giving the impression that memory is running out, which makes people nervous. In fact, there is absolutely no need to worry. When a Linux server detects insufficient memory, it will automatically clean the cached area, release memory, then continue to increase the cache while free continues to decrease. Therefore, those methods of manually reducing memory usage are really just a quick fix for peace of mind, heh.
 
 

 

 

echo 1 > /proc/sys/vm/drop_caches can temporarily clear cache and buffer

On RHEL5 and Ubuntu (not on RHEL4), you can directly modify the kernel file
# /etc/sysctl.conf
vm.drop_caches = 1

#sysctl -p 

 

 

 $ sync
$ free -m
             total       used       free     shared    buffers     cached
Mem:         32096      30084       2011          0        590      26162
-/+ buffers/cache:       3332      28764
Swap:        34287          0      34287
echo 3 > /proc/sys/vm/drop_caches
free -m

 
I originally thought it was Oracle cache that was full,
so in SQLPLUS I entered
ALTER SYSTEM FLUSH BUFFER_CACHE;
alter system flush shared_pool;
 but the effect wasn’t obvious after checking.
 Then I tried clearing the Linux ARP cache.
Method 1: arp -n|awk '/^[1-9]/ {print "arp -d "$1}' | sh
Clears all ARP cache, recommended!
Method 2: for((ip=2;ip<255;ip++));do arp -d 192.168.0.$ip &>/dev/null;done
Clears all cache for the 192.168.0.0 network segment.
Method 3: arp -d IP
This clears the ARP cache for a single IP.
Note: All of the above require root privileges, especially the last one. If not executed as root, change it to:
arp -n|awk '/^[1-9]/ {print "arp -d "$1}' | sudo sh

Leave a Comment

Your email address will not be published.