When we use free-h to look at the system memory, there are times when we find that the buff/cache is high

> free -h
Copy the code

Available Indicates the memory available to applications

What is a buff

A Buffer Cache is an I/O Cache that is used to Buffer memory and hard disk. It is the read/write Buffer for I/O devices. According to the disk read and write design, the scattered write operations are centralized, reduce disk fragmentation and hard disk repeated seek, so as to improve system performance.

What is the cache

Page cache (Page cache) is a type of cache used to buffer between THE CPU and memory. It is the cache of the file system.

Save the data you have read, and do not read the hard disk if it is hit (find the data you need). The data is organized according to the frequency at which it is read, putting the most frequently read content in the easiest place to find it, and moving the unread content to the back until it is deleted.

They all occupy memory. Both are data in RAM. Basically, the buff is about to be written to disk, and the cache is read from disk.

The amount of memory that is actually being used by the process is calculated as used-buff/cache, and the amount of memory that can be used after the buff/cache is freed is free+buff/cache. Usually, when we access files frequently, the buff/cache usage increases.

removebuff/cache

Manually remove

> sync
> echo 1 > /proc/sys/vm/drop_caches
> echo 2 > /proc/sys/vm/drop_caches
> echo 3 > /proc/sys/vm/drop_caches
Copy the code
  • Sync: writes all unwritten system buffers to disks, including modified I-Nodes, delayed block I/O, and read/write mapping files
  • Echo 1 > /proc/sys/vm-drop_caches: clears the Page cache
  • Echo 2 > /proc/sys/vm/drop_caches: clears and reclaims objects in the slab allocator (including the directory entry cache and inode cache). The SLAB allocator is a mechanism for managing memory in the kernel, and many implementations of cached data use Pagecache.
  • Echo 3 > /proc/sys/vm/drop_caches: clears cache objects in the Pagecache and slab allocators.

The default value for /proc/sys/vm-drop_caches is 0

Clearing a Scheduled Task

> vim clean.sh #! /bin/bash# clear the cache every 2 hours echo "start to clear the cache" sync; sync; Sync # Write to hard drive, Echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/vm/drop_caches echo 3 > /proc/sys/vm/drop_caches > chmod +x clean.sh > crontab -e # 0 */2 * * */ opt/clean.sh is executed every two hoursCopy the code
  • Set the crond to start and start upon startup
  • systemctl start crond.service
  • systemctl enable crond.service

Link :rumenz.com/rumenbiji/l… Wechat official Account: entry station