The linux clean memory command is described in detail

  • 2020-05-13 04:24:50
  • OfStack

linux clean memory command

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:


# 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 no need to deliberately release cache that is already in use in general. This cache content can increase the file and read and write speed.
First, how does the free command look at 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 cache+ applications
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

Line 3 shows the use of swap:

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, delayed block I/O, and read-write mapping files.)


[root@server test]# echo 3 > /proc/sys/vm/drop_caches
[root@server test]# cat /proc/sys/vm/drop_caches

! 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

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: