In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you what is the use of the free command about the Linux system. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The free command shows the usage of system memory, including physical memory, swap memory (swap), and kernel buffer memory.
If you add the-h option, the output will be much friendlier:
Sometimes we need to continuously observe the state of memory, we can use the-s option and specify the number of seconds between:
$free-h-s 3
The above command outputs memory usage every 3 seconds until you press ctrl + c.
Because the free command itself is relatively simple, this article will focus on how to understand the current memory usage of the system through the free command.
Introduction to the output Let's first explain the output: the Mem line (the second line) is memory usage. The Swap line (the third line) is the swap space usage. The total column shows the total physical memory and swap space available to the system. The used column shows the physical memory and swap space that has been used. The free column shows how much physical memory and swap space is available. The shared column shows the amount of physical memory used by the share. The buff/cache column shows the amount of physical memory used by buffer and cache. The available column shows the amount of physical memory that can also be used by the application.
I think the above output can help us understand the memory state of the system only after we understand some basic concepts.
Buff/cache starts with a question: buffer and cache are supposed to be two types of memory, but why do the free commands put them together? We need to do some preparatory work to answer this question. Let's first clarify the meaning of buffer and cache.
Buffer refers to buffer cache in the operating system and is generally translated as "buffer" in Chinese. To understand buffers, you must clarify two other concepts: "sectors" and "blocks". A sector is the smallest addressing unit of a device, also known as a "hard sector" or "device block". A block is the smallest addressing unit of a file system in the operating system, also known as a "file block" or "Icano block". Each block contains one or more sectors, but the size cannot exceed one page, so a page can hold one or more blocks in memory. When a block is called into memory, it is stored in a buffer. Each buffer corresponds to a block, which is equivalent to the representation of the disk block in memory (the following figure is from the Internet):
Note that buffer cache only has the concept of blocks, not files. It just moves blocks on disk directly to memory, regardless of what format files are stored in the blocks.
Cache refers to page cache in the operating system and is generally translated as "page cache" in Chinese. The page cache is a disk cache implemented by the kernel. It is mainly used to reduce the number of I _ big O operations on the disk. Specifically, the access to the disk is changed into the access to the physical memory by caching the data in the disk into physical memory. The page cache caches memory pages. The pages in the cache come from reading and writing to normal files, block device files (which means buffer cache), and memory-mapped files. Page cache caching of ordinary files can be understood like this: when the kernel wants to read a file (such as / etc/hosts), it first checks to see if the file's data is already in the page cache. If it is, give up access to the disk and read it directly from memory. This behavior is called a cache hit. If the data is not in the cache, or the cache has not been hit, the kernel has to schedule the block Imax O operation to read the data from the disk. The kernel then puts the read data into the page cache. The target of this cache is files that are recognized by the file system (such as / etc/hosts). Page cache caching of block device files is the buffer cahce we introduced earlier. Because independent blocks are also stored in the page cache through the buffer (the buffer is eventually hosted by the page cache).
We should be clear at this point: both buffers and page caches are implemented in the same way. A buffer is just a conceptually special page cache. So why isn't the free command directly called cache rather than written as buff/cache? This is because the implementation of buffers and page caches are not inherently uniform. They are unified only in the linux kernel 2.4. Earlier kernels had two separate disk caches: the page cache and the buffer cache. The former caches the page, while the latter caches the buffer. When you know these stories, the names of the columns in the output may no longer be important.
Free and available have a free column and an available column in the output of the free command. What is the difference between the two? Free is the real amount of physical memory that has not yet been used. As for available, it is interesting to see the amount of memory available from the perspective of the application. In order to improve the performance of disk operations, the Linux kernel consumes part of the memory to cache disk data, which is the buffer and cache we introduced. So for the kernel, both buffer and cache belong to memory that has already been used. When an application needs memory, if there is not enough free memory available, the kernel reclaims memory from buffer and cache to satisfy the application's request. So from an application point of view, available = free + buffer + cache. Please note that this is only an ideal method of calculation, and the actual data often have large errors.
Swap space (swap space) swap space is an area on disk that can be a partition or a file. So the specific implementation can be swap partition or swap file. When the system's physical memory is tight, Linux will save the infrequently accessed data in memory to swap, so that the system will have more physical memory to serve each process, and when the system needs to access the content stored on swap, it will load the data on swap into memory, which is often called swap out and swap in. Swap space can alleviate the lack of memory to some extent, but it needs to read and write disk data, so the performance is not very high.
Today's machines are generally not short of memory, if the system defaults or uses swap, will it slow down the performance of the system? In theory, yes, but in practice it is not very likely. And the kernel provides a parameter called swappiness to configure the urgency of moving data that is not commonly used in memory to swap. The range of values for this parameter is 0: 0100. 0 tells the kernel not to move memory data to swap as much as possible, that is, to do so only if necessary, while 100 tells the kernel to move infrequently accessed data in memory to swap whenever possible. On ubuntu systems, the default value for swappiness is 60. If we think we have enough memory, we can set swappiness in the / etc/sysctl.conf file:
Vm.swappiness=10 if the system is out of memory, you need to set the size of the swap space according to the size of the physical memory. There is a wealth of information about specific strategies on the Internet, and the author will not repeat them here.
/ proc/meminfo file actually all the information in the free command comes from the / proc/meminfo file. The / proc/meminfo file contains more and more raw information, but it doesn't look very intuitive:
$cat / proc/meminfo
Students who are interested can view this file directly.
The free command is a simple and complex command. It is simple because the command has few parameters and the output is clear. It's complicated because there are more obscure operating system concepts behind it, and if you don't know these concepts, you don't get much valuable information even if you look at the output of the free command.
What are the versions of Linux? the versions of Linux are Deepin, UbuntuKylin, Manjaro, LinuxMint, Ubuntu and so on. Among them, Deepin is one of the best-developed Linux distributions in China; UbuntuKylin is a derivative release based on Ubuntu; Manjaro is a Linux release based on Arch; LinuxMint's default Cinnamon desktop is similar to Windows XP's easy-to-use; Ubuntu is the Linux operating system based on desktop applications.
The above is the use of the free command of the Linux system shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.