Due to the kernel mechanism of LINUX, under normal circumstances there is no need to release the used cache. These cached contents can increase the file and read and write speed. But in some cases, if all physical memory is used, there will be performance problems when there is sudden physical memory consumption, such as mysql database server.
First, let's talk about how the free command looks at memory
[root@tangj proc]# free total used free shared buffers cached Mem: 515588 295452 220136 0 2060 64040 -/+ buffers/cache: 229352 286236 Swap: 682720 112 682608
The first line describes the memory used by the system from a global perspective:
total - total physical memory
used - used memory, in general, this value will be relatively large, because this value includes the memory used by cache + application
free - completely unused memory
shared - application shared memory
buffers - cache, mainly used for directory, inode value, etc. (ls large directory can see this value increase)
cached - cache, for open files
note:
total=used+free
used=buffers+cached (maybe add shared also)
The second line 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 - the size of all memory available to the application, free plus the cache value
note:
-buffers/cache=used-buffers-cached
+buffers/cache=free+buffers+cached
The third line indicates the use of swap:
used - has been used
free - unused
Note, it is best to sync before releasing to prevent data loss.
Cache memory release (running once is only temporary recovery, you can use crontal to periodically execute sync and the corresponding cleanup statement to automatically clean up, the following operations are valid for kernels above kernel 2.6.16):
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, remember to sync before releasing to prevent data loss.
After modifying /etc/sysctl.conf and adding the following options, the memory will not continue to increase
vm.dirty_ratio = 1 vm.dirty_background_ratio=1 vm.dirty_writeback_centisecs=2 vm.dirty_expire_centisecs=3 vm.drop_caches=3 vm.swappiness =100 vm.vfs_cache_pressure=163 vm.overcommit_memory=2 vm.lowmem_reserve_ratio=32 32 8 kern.maxvnodes=3
The above settings are relatively rough, so that the role of the cache is basically unable to play. Appropriate adjustments to the condition of the machine are required to find the best compromise.
/proc/sys/vm/dirty_ratio
This parameter controls the size of the file system write buffer of the file system. The unit is a percentage, which indicates the percentage of the system memory. It indicates that when the write buffer uses the system memory, it starts to write data to the disk. Increasing it will use more system memory for disk write buffering, and can also greatly improve system write performance. However, when you need continuous and constant writing, you should lower its value. Generally, it defaults to 10 on startup. Set 1 to accelerate the program speed
/proc/sys/vm/dirty_background_ratio
This parameter controls when the filesystem's pdflush process flushes the disk. The unit is a percentage, indicating the percentage of system memory, which means that when the write buffer uses how much system memory, pdflush starts to write data to the disk. Increasing it will use more system memory for disk write buffering, and can also greatly improve system write performance. However, when you need continuous and constant writing, you should reduce its value. Generally, the default value is 5.
/proc/sys/vm/dirty_writeback_centisecs
This parameter controls the running interval of the kernel's dirty data flushing process pdflush. The unit is 1/100th of a second. The default value is 500, which is 5 seconds. If your system is writing continuously, it is actually better to lower this value so that the spiked writes can be flattened into multiple writes.
/proc/sys/vm/dirty_expire_centisecs
After this parameter declares how "old" the data in the Linux kernel write buffer is, the pdflush process begins to consider writing to disk. The unit is 1/100 of a second. The default is 30000, that is, 30 seconds of data is old, and the disk will be refreshed. For particularly overloaded write operations, it is good to shrink this value appropriately, but it can't shrink too much, because shrinking too much will also cause the IO to increase too quickly. It is recommended to set it to 1500, which is 15 seconds old.
/proc/sys/vm/drop_caches
free used cache
/proc/sys/vm/page-cluster
This file indicates the number of pages written when writing to the swap area, 0 means 1 page, 1 means 2 pages, and 2 means 4 pages.
/proc/sys/vm/swapiness
This file represents the extent to which the system performs swapping behavior, the higher the value (0-100), the more likely disk swapping will occur.
/proc/sys/vm/vfs_cache_pressure
This file represents the kernel's tendency to reclaim memory used for directory and inode caches
/etc/sysctl.conf vm.dirty_ratio = 1 vm.dirty_background_ratio=1 vm.dirty_writeback_centisecs=1 vm.dirty_expire_centisecs=3 vm.drop_caches=3 vm.swapiness=100 vm.vfs_cache_pressure=133 vm.dirty_ratio = 5 #dft 20 % vm.dirty_background_ratio =5 #dft 10 % vm.dirty_writeback_centisecs=100 #dft 500 is 5s vm.dirty_expire_centisecs=300 #dft 30000 is 30s vm.drop_caches=3 #dft 0 vm.swappiness=100 #dft 60 vm.vfs_cache_pressure=133 #dft 100 vm.min_free_kbytes=204800 #200M mem save simple sysctl -w net.ipv4.route.flush=1