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

The reason why Linux system wants to eat "memory"

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the Linux system to eat "memory" reasons, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Check the memory usage of the resource manager under Windows. If the utilization rate reaches more than 80%, running large programs will feel that the system is not smooth, because using swap partitions in case of memory shortage and frequently swapping in and out pages from disk will greatly affect the performance of the system.

When we use the free command to look at the memory usage of the Linux system, we will find that memory usage has been at a high level, even if the system is not running much software at this time. 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 also the characteristic of its memory management.

1. Free command

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

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

Mem: total (total) = 31405MB, used (used) = 30254MB, free (free) = 1150MB, shared (shared memory) = 12514MB, buffers = 1122MB, cached = 25424MB

Note: the first four items are easy to understand. The difference between buffer and cache is:

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

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

The third line of the output represents the result of-/ + buffers/cache based on the second line:

-buffers/cache used = Mem used-buffers-cached = 30524MB-1122MB-25424MB = 3708MB + buffers/cache free = Mem free + buffers + cached = 1150MB + 1122MB + 25424MB = 27696MB

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

Swap:total (total) = 15775MB used (use) = 596MB free (idle) = 4095MB

two。 Classification of memory

Here, memory is divided into three categories, and there are different names for its usage from the point of view of users and operating systems:

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

Back to the output of the free command, the result of the third line should be understood, and the numbers in this line represent the usage of the system memory from the user's point of view. So, if you use the top or free command to see how much memory is left on the system, you should add the free memory to buffer/cached 's memory, which is the actual system's free memory.

3. Benefits of buffers/cached

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

And Linux will make full use of these free memory, the design idea is that it is better to cache more data when the memory is idle, and so the next time the program accesses the data again, it will be faster, but if the program wants to use memory and the system is out of memory, instead of using swap partitions, it will quickly recycle part of the cache and leave it to the user program.

Therefore, it can be seen that buffers/cached is really beneficial and harmless, and the real disadvantages may give users the illusion that Linux consumes memory! In fact, Linux does not eat your memory, as long as the swap partition is not used.

4. Experimental test: read a large file one after another, and compare the time of two reads

(1) generate a large 1G file first

Dd if=/dev/zero of=bigfile bs=1M count=1000

(2) clear the cache

Be sure to change it back to 0 after changing to 3 here, or the following test results will be inaccurate.

(3) read this file and test the time spent

It takes 24 seconds

(4) read the file again and test the time spent

It takes 17 seconds

As can be seen from the above, it takes about 24 seconds to read the 1G file for the first time, but only 17 seconds for the second time, which is a drop.

Thank you for reading this article carefully. I hope the article "the reason why the Linux system eats the memory" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Servers

Wechat

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

12
Report