Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Linux memory, Swap, Cache, Buffer

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge of what Linux memory, Swap, Cache and Buffer are. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

In Linux systems, buffer and cache are the space allocated in memory, and SWAP is the space on disk.

1. View Linux memory Linux memory, Swap, Cache, BufferLinux memory, Swap, Cache, Buffer through the free command

Total: total memory size.

Used: the amount of memory that has been used (this includes cached and buffers and shared sections).

Free: the amount of free memory.

Shared: shared memory between processes (usually not used and can be ignored).

Buffers: things written in memory are cached so that requests are responded quickly, and later data is periodically brushed to disk.

Cached: the amount of content that is cached after reading in memory (this part is for quick return on the next query).

-/ + buffers/cache is regarded as two parts:

-buffers/cache: the amount of memory being used (note that it is not the used section, because buffers and cached are not being used, and organizations and people need them to be freed), with a value of = used-buffers-cached.

+ buffers/cache: the amount of memory available (similarly not the part represented by free), with a value of = free+buffers+cached.

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

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

2. Buffer and Cache introduce Linux memory, Swap, Cache, BufferLinux memory, Swap, Cache, Buffer

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

Cache is mainly designed for read operations, but the concept of Cache may be easily confused. I understand that CPU itself has Cache, including first-level cache, second-level cache and third-level cache. We know that all instruction operations of CPU are docked with memory, and the processing power of CPU is much higher than memory speed, so in order not to let CPU resources idle, Intel and other companies have integrated some Cache in CPU, but after all, they can't put too many circuits in it. So this part of the Cache is not very large, mainly used to store some commonly used instructions and data, the real majority of Cache data should take up memory space to cache the requested data, that is, the above Cached part (this part is purely personal understanding, whether it is correct or not 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 centralize write operations, reduce disk fragmentation and repeated addressing process of hard disk, and improve performance.

Within the Linux system, there is a daemon that periodically empties the contents of the Buffer and writes it to the hard disk, which is also triggered when the sync command is executed manually.

3. Common symptoms

Symptom 1: frequently accessing files in Linux, physical memory runs out quickly, and cached is growing all the time.

Explanation: Linux caches the requested data in cache. The advantage is that the processing speed of CPU is much higher than that of memory, so it can quickly return the hit result from cache when CPU communicates with memory.

Symptom 2: Swap is occupied.

Explanation: there may not be enough memory to take up Swap, so Swap can be used as an indicator for server monitoring to attract attention.

4. Manually clean up Swap and buffers/cache

(1) Clean up Swap

Swapoff-a & & swapon-a

Operation instructions: if Swap has been used and there is still space under + buffers/cache, when performing the swapoff-an operation, it will trigger swapping the contents of Swap to memory, and the data will not be lost.

(2) Clean up buffers/cache:

Sync; sync; sync;&& echo 3 > / proc/sys/vm/drop_caches sleep 2 echo 0 > / proc/sys/vm/drop_caches

Operation instructions:

Sync- > write the inside of the cache back to the hard disk

Echo 3 > / proc/sys/vm/drop_caches- > modify the value of drop_caches to 3, and default to 0. If changed to 3, the cached contents will be cleaned up.

Sleep 2-> wait a minute to prevent the previous step from being finished.

Echo 0 > / proc/sys/vm/drop_caches-> change back to the default value

These are all the contents of the article "what is Linux memory, Swap, Cache and Buffer?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report