Home > System Tutorial > LINUX > body text

How to use SWAP, Cache and Buffer in Linux reasonably?

WBOY
Release: 2024-02-13 15:30:17
forward
1173 people have browsed it

In the Linux system, the three concepts of SWAP, Cache and Buffer often appear in front of us. What do they mean respectively? Simply put, SWAP refers to the virtual memory swap area, Cache refers to the buffer area, and Buffer refers to the input and output buffer area. Their use in Linux systems plays a vital role in the performance of the system. However, different application scenarios and different applications occupy and use these three cache areas differently. How can we use them more rationally? Next, this article will introduce it to you in detail. How to use SWAP, Cache and Buffer in Linux reasonably?

**1. View Linux memory through free command

**

How to use SWAP, Cache and Buffer in Linux reasonably?

total: Total memory size.

used: The memory size that has been used (this includes cached, buffers and shared parts).

free: Free memory size.

shared: Shared memory between processes (generally not used, can be ignored).

buffers: The things written in the memory are cached, so that the request can be responded to quickly, and the subsequent data is regularly flushed to the disk.

cached: The size of the cached content in the memory after reading (this part is for quick return during the next query).

-/ buffers/cache is considered as two parts:

-buffers/cache: The size of the memory being used (note that it is not the used part, because buffers and cached are not in use, organizations and people need to ensure that they can be released), its value = used-buffers-cached.

buffers/cache: The available memory size (similarly not the part represented by free), its value = free buffers cached.

Swap: The size of the swap partition on the hard disk.

The purpose of the design is that when the available memory represented by the buffers/cache mentioned above has been used up, after new read and write requests come, part of the data in the memory will be written to the disk, thereby treating part of the disk space as virtual memory to use.

2. Introduction to Buffer and Cache

How to use SWAP, Cache and Buffer in Linux reasonably?

Cache (cache) is designed to increase data exchange between CPU and memory, and Buffer (buffer) is designed to increase data exchange between memory and hard disk (or other I/O devices).

Cache is mainly designed for read operations, but the concept of Cache may be easily confused. I understand that the CPU itself has Cache, including first-level cache, second-level cache, and third-level cache. We know that all instruction operations of the CPU are connected They are all memory, and the processing power of the CPU is much higher than the memory speed, so in order to prevent the CPU resources from being idle, Intel and other companies have integrated some Cache inside the CPU, but after all, they cannot put too many circuits in it, so this part of the Cache is not It is very large and is mainly used to store some commonly used instructions and commonly used data. Really, most of the Cache data should occupy the memory space to cache the requested data, that is, the Cached part above (this part is purely personal understanding, correct and Whether it remains to be verified).

Buffer is mainly designed for write operations, and more specifically for write operations between memory and hard disk. The purpose is to concentrate write operations and reduce disk fragmentation and repeated hard disk addressing processes. Improve performance.

There is a daemon process inside the Linux system that regularly clears the contents of the Buffer and writes it to the hard disk. The above operation will also be triggered when the sync command is manually executed.

3. Common symptoms

Symptom 1: Files are frequently accessed in Linux, the physical memory is quickly used up, and cache keeps growing.

Explanation: Linux will cache each requested data in the cache. The advantage is that the processing speed of the CPU is much higher than that of the memory, so when the CPU and memory communicate, the hit result can be quickly returned from the cache.

Symptom 2: Swap is occupied.

Explanation: Swap may be occupied when the memory is insufficient, so Swap can be used as an indicator of server monitoring to attract attention.

4. Manually clean up Swap and buffers/cache

(1) Clean up Swap

swapoff -a && swapon -a 
Copy after login

Operation instructions: If Swap has been used, and there is still space in buffers/cache after clearing it, performing the swapoff -a operation will trigger the swapping of the contents of Swap into memory, and the data will not be lost.

(2) Clean buffers/cache:

sync; sync; sync;&& echo 3 >/proc/sys/vm/drop_caches  
sleep 2  
echo 0 > /proc/sys/vm/drop_caches 
Copy after login

Instructions:

sync–>Write the cached content back to the hard disk;

echo 3 >/proc/sys/vm/drop_caches–>Modify the value of drop_caches to 3. The default is 0. If it is changed to 3, the system will clear the cached content;

sleep 2 –> Wait a moment to prevent the previous step from being completed;

echo 0 >/proc/sys/vm/drop_caches –> Change back to default value

In general, the three cache areas of SWAP, Cache and Buffer are crucial parts of the Linux system, and they have a direct impact on the performance and stability of the system. Properly controlling and using them can make the system run more stable and efficient, thus improving the user experience. I hope this article will help you understand the use of these three cache areas in Linux systems. If you have anything to add or have questions, please leave a message for communication.

The above is the detailed content of How to use SWAP, Cache and Buffer in Linux reasonably?. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template