1. Run the free command to view Linux memory

  • Total: indicates the total memory size.
  • Used: The amount of memory that has been used (this includes cached and buffers and shared parts).
  • Free: indicates the free memory size.
  • Shared: Shared memory between processes (usually not used and can be ignored).
  • Buffers: Buffers are stored in memory, allowing rapid response to requests, and subsequent data is periodically flushed to disk.
  • Cached: The size of the memory used to read the cached contents (this part is used for quick return on the next query).

-/+ buffers/cache

  • -buffers/cache: Specifies the size of memory in use (not the used part, because buffers and cached are not in use, and organizations and people need them to be freed), and its value =used-buffers-cached.
  • + Buffers /cache: Available memory size (again not part of free) =free+buffers+cached.

Swap: Used size of the Swap partition on a hard disk.

If buffers/ buffers are used up, a new write request will write some of the data in the memory to the disk, which will be used as virtual memory.

2. Buffer and Cache

The Cache is designed to speed up the exchange of data between the CPU and memory, and the Buffer is designed to speed up the exchange of data between memory and the hard disk (or other I/O devices). Cache is mainly designed for read operations, but the concept of Cache may be confusing. I understand that the CPU itself has Cache, including level 1 Cache, level 2 Cache, and level 3 Cache. We know that all CPU instructions and operations are connected to the memory, and the PROCESSING capacity of the CPU is much higher than the memory speed, so in order not to idle CPU resources. Intel and other companies integrate some caches in the CPU, but after all, they cannot store too many circuits in the Cache, so this part of the Cache is not very large. It is mainly used to store some commonly used instructions and data. Most of the data in the Cache should occupy the memory space to Cache the requested data. That is the Cached part above (this part is a personal interpretation, but it needs to be checked). Buffers are designed primarily for writes, and more specifically for writes between memory and disk, to centralize writes, reduce disk fragmentation and disk addressing, and improve performance. In Linux, a daemon periodically clears the Buffer and writes it to the disk. This operation is also triggered when the sync command is executed manually.

3. Common symptoms

Symptom 1: Files are accessed so frequently in Linux that you quickly run out of physical memory and cached keeps growing. Linux caches each requested data in the cache. The advantage is that the CPU processing speed is much faster than the memory, so the communication between the CPU and memory can quickly return the result from the cache. Symptom 2: The Swap is occupied. Note: Memory may be insufficient to occupy Swap, so Swap can be used as an indicator of server monitoring, to attract attention.

4. Manually clear Swap and buffers/cache

(1) clean the Swap

swapoff -a && swapon -a
Copy the code

Operation description: If a Swap has been used and the buffers/cache is cleared, the swapoff -a operation triggers the Swap content to the memory, preventing data loss. (2) clear the buffers/cache:

sync; sync; sync; && echo 3 >/proc/sys/vm/drop_caches sleep 2 echo 0 > /proc/sys/vm/drop_cachesCopy the code

Operation instructions:

Sync --> write cached contents back to disk; Echo 3 > /proc/sys/vm. drop_caches- > Change the value of drop_caches to 3. The default value is 0. Sleep 2 --> < span style = "box-sizing: border-box! Important; Echo 0 > /proc/sys/vm. drop_caches --> Change the default valueCopy the code

5, summary

According to the above analysis, a small amount of free physical memory does not necessarily mean that the system is running badly, because the cache and buffer parts of the memory can be reused at any time. In a sense, these two parts of memory can also be regarded as extra free memory. If swap is frequently invoked, bi and BO are not 0 for a long time, which determines whether memory resources are tight. When looking at resources in free, focus on the value of -/+ buffers/cache to see if there is enough memory.