Summary of linux memory cleanup and release command

  • 2020-05-13 04:20:49
  • OfStack

This article describes the linux memory cleanup/release command as follows:

1. Clean up the memory usage before


free -m

2. Start cleaning up


echo 1 > /proc/sys/vm/drop_caches

3. Memory usage after cleaning


free -m

4. Done!

View the memory number command:


dmidecode | grep -A16 "Memory Device$"
# sync
# echo 1 > /proc/sys/vm/drop_caches
 echo 2 > /proc/sys/vm/drop_caches
 echo 3 > /proc/sys/vm/drop_caches

cache release:

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

Note: it is better to use sync1 before release to prevent data loss.

Because of LINUX's kernel mechanism, there is generally no need to deliberately release cache that is already in use. This cache content can increase the file as well as the read and write speed.

First, what does the free command say about memory


[root@yuyii proc]# free

total  used  free   shared buffers cached
Mem: 515588 295452 220136 0   2060  64040
-/+ buffers/cache: 229352 286236
Swap: 682720 112 682608

Where, line 1 describes the memory used by the system from a global perspective:

total -- total physical memory
used - used memory, 1 normally this value is larger because it includes the memory used by the cache+ application
free -- completely unused memory
shared -- application Shared memory
buffers -- cache, mainly used for directories,inode values, etc. (you can see this increase in the ls large directory)
cached -- cache, for files that have been opened

note:


  total=used+free
  used=buffers+cached (maybe add shared also)

Line 2 describes the memory usage of the application:

The previous value represents -buffers /cache -- the amount of memory used by the application, used minus the cache value

The latter value represents +buffers/cache -- all available memory for the application, free plus the cache value

note:


-buffers/cache=used-buffers-cached
  +buffers/cache=free+buffers+cached

 The first 3 Line said swap The use of: 

used - have been used
free - not used

Manually execute the sync command (description: the sync command runs the sync subroutine. If you must stop the system, run the sync command to ensure file system integrity. The sync command writes all unwritten system buffers to disk, including modified i-node, deferred block I/O, and read-write mapping files.)


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

! Will/proc sys vm/drop_caches value is set to 3

About/proc/sys/vm/drop_caches usage is illustrated below

/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.

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


Related articles: