Detailed description of the characteristics of Linux system memory management

In Windows, Explorer looks at the memory usage. If the usage rate reaches 80% or more, you can feel that the system is not smooth after running a large program, because the swap partition is used in the case of memory shortage, and it is frequently changed from the disk. Incoming and swapping out pages can greatly affect the performance of the system. When we use the free command to view the Linux system memory usage, we will find that memory usage has been at a high level, even if the system does not run much software. This is the difference between Windows and Linux in memory management. At first glance, the Linux system eats our memory (Linux ate my ram), but in fact this is the characteristics of its memory management.

Free command introduction

The following is the result of using the free command to view the memory of our lab file server. The -m option means using MB as the unit:

The second line of output indicates system memory usage:

Mem: total = 3920MB,

Used (used) = 1938MB,

Free (idle) = 1982MB,

Shared (shared memory) = 0MB,

Buffers = 497MB,

Cached = 1235MB

Note: The first four items are better understood. Buffer and cache can't find the right words to translate. The difference is:

A buffer is something that has yet to be "written" to disk.

A cache is something that has been "read" from the disk and stored for later use.

That is, the buffer is used to store the data to be output to the disk, and the cache is read from the disk and stored in the memory for future use. They are all introduced to provide IO performance.

The third line of the output is obtained on the basis of the second line -/+ buffers/cache:

– buffers/cache used = Mem used – buffers – cached = 1938MB – 497MB – 1235MB = 205MB

+ buffers/cache free = Mem free + buffers + cached = 1982MB + 497MB + 1235MB = 3714MB

The third line of output indicates the use of the swap partition:

Swap:total (total) = 4095MB

Used = 0MB

Free (idle) = 4095MB

Since the current memory of the system is still sufficient, the swap partition is not used.

The result of the above output is more difficult to understand may be the third line, why should you show this line of data to the user? What is the memory usage minus the memory of the system buffer/cached? What does the system free memory plus buffer/cached memory mean?

Memory classification

We divide memory into three categories, which have different names for their use from the perspective of users and operating systems:

Memory that is You'd call it Linux calls it
Taken by applications Used Used
Available for applications, and used for something Free Used
Not used for anything Free Free

Something in the above table represents the memory of the "buffers/cached" in the free command. Since this memory is actually used from the perspective of the operating system, if the user wants to use it, this memory can be quickly recycled by the user program. Used, so this memory should be classified as idle from the user's perspective.

Going back to the result of the free command output, the result of the third line output should be understood. The number in this line indicates the usage of the system memory from the user's perspective. Therefore, if you use the top or free command to see how much memory is left in the system, you should add the free memory to the buffer/cached memory, which is the actual system free memory.

Buffers/cached benefits

Linux

Memory management has done a lot of careful design, in addition to caching dentry (for VFS, speed up file path name to inode conversion), also adopted two main Cache methods: Buffer Cache and Page Cache, the purpose is to improve disk IO Performance. Reading data from a low-speed block device is temporarily stored in memory, even if the data is no longer needed at the time, but the next time the application accesses the data, it can be read directly from the memory, bypassing the low speed. The block device thus improves the overall performance of the system.

Linux will make full use of these free memory. The design idea is that it is better to use more memory to cache some data. The next time the program accesses the data again, the speed is faster, and if the program uses memory and the memory in the system is insufficient. Instead of using swap partitions, you can quickly reclaim partial caches and leave them to the user program.

Therefore, it can be seen that buffers/cached is really beneficial and harmless. The real harm may cause users to have an illusion - Linux consumes memory! In fact, Linux does not eat your memory. As long as you have not used the swap partition, you should be thankful that your memory is running low. Because Linux caches a lot of data, maybe you will benefit from it next time!

Experimental proof

The following conclusions are verified by experiments:

We have read a large file and compared the practice of two readings:

1. First generate a large 1G file

2. Empty the cache

3. Read this file and test the time spent

4. Read the file again and test the time spent

From the above, it takes about 18 seconds to read the 1G file for the first time, and only 0.3s for the second time, which is 60 times higher!

washing machine motor

Washing Machine Motor,Spin Motor Of Aluminium Wire,Washing Machine Motor Shaft,Automatic Washing Machine Spin Motor

WUJIANG JINLONG ELECTRIC APPLIANCE CO., LTD , https://www.jinlongmotor.com