Free command introduction! Classification of memory!

Check the memory usage in Windows Explorer. 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 when the memory is scarce, and the disk is frequently changed. Pages in and out will greatly affect the performance of the system. When we use the free command to view the memory usage of the Linux system, we will find that the memory usage has been at a high level, even if the system does not run much software at this time.

This is the difference between Windows and Linux in memory management. At first glance, the Linux system eats up our memory (Linux ate my ram), but in fact this is also the characteristic of its memory management.

Free command introduction

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

The second line of output indicates the usage of system memory:

Mem: total = 3920MB,

used = 1938MB,

free = 1982MB,

shared (shared memory) = 0MB,

buffers = 497MB,

cached = 1235MB

Note: The first four items are relatively easy to understand, buffer and cache cannot find suitable words to translate. The difference between them 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 the data that 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 output represents the result of -/+ buffers/cache on the basis of the second line:

– 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 usage of the swap partition:

Swap: total (total) = 4095MB

used = 0MB

free = 4095MB

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

The result of the above output may be the third row that is more difficult to understand. Why do you want to show this row of data to the user? What does the memory usage minus the system buffer/cached memory mean? What does the free memory of the system and the memory of buffer/cached mean?

Classification of memory

We divide memory into three categories, which are called differently from the perspective of users and operating systems:

The something in the above table represents the "buffers/cached" memory in the free command. This memory is indeed used from the perspective of the operating system, but if the user wants to use it, this memory can be quickly recycled by the user program. Use, so this memory should be classified as free from the user's point of view.

Going back to the output of the free command again, the output of the third line should be understandable. The numbers in this line represent the usage of system memory from the user's point of view. 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, and that is the actual free memory of the system.

Buffers/cached benefits

Linux memory management has done a lot of careful design. In addition to caching dentry (used for VFS to accelerate the conversion of file path names to inodes), it also adopts two main Cache methods: Buffer Cache and Page Cache, the purpose is to improve the disk IO performance. Data read from a low-speed block device will be temporarily stored in the memory. Even if the data is no longer needed at the time, it can be read directly from the memory when the application program accesses the data next time, thereby bypassing the low-speed Block devices to improve the overall performance of the system.

Linux will make full use of these free memory. The design idea is that if the memory is free, it is better to use it to cache more data. The next time the program accesses the data again, the speed will be faster, and if the program wants to use the memory and the memory in the system is insufficient , At this time, instead of using the swap partition, part of the cache is quickly recovered and reserved for user programs.

Therefore, it can be seen that buffers/cached is really beneficial and harmless. The real disadvantage may give users an illusion that Linux consumes memory!

In fact, Linux has not eaten your memory. As long as you have not used the swap partition, you should feel fortunate that your memory is running out. Because Linux caches a lot of data, you might benefit from it next time.

Experimental proof

The following experiments are used to verify the above conclusions:

We read a large file successively, and compare the practice of reading it twice:

1. First generate a 1G large file

2. Clear the cache

3. Read this file and test the time consumed

4. Read the file again and test the time consumed

It can be seen from the above that it takes about 18s to read this 1G file for the first time, and the second time it takes only 0.3s to read this 1G file, which is a full 60 times improvement!

Ningbo Autrends International Trade Co.,Ltd. , https://www.mosvapor.com