Calculate Linux system memory utilization correctly

Source: Internet
Author: User

Reference: https://blog.gesha.net/archives/406/

The example in the diagram is typical: Most Linux systems will find that free (the rest) has little memory and no more programs or services. The correct explanation for the above scenario is that Linux has a different memory management mechanism than Windows. The specific mechanism we need to know is that the idea of a Linux memory management mechanism includes (not to say) the maximization of memory utilization. The kernel will apply the remaining memory to cached, while cached does not belong to the free category. When the system runs for a long time, it will be found that the cached is very large, for the frequent file read and write operation of the system, this phenomenon will be more obvious.   Visually, at this time free memory will be very small, but does not mean that the available memory is small, when a program needs to request a large amount of memory, if free memory is not enough, the kernel will be part of the cached memory, reclaimed memory redistribution to the application. So for Linux systems, the memory that can be allocated is not just free memory, but also cached's memory (in fact includes buffers).   1 the current memory usage by collecting the meminfo files in the/proc file system regularly: the proc file system is a pseudo file system that only exists in memory and does not occupy external memory space. It provides an interface for accessing system kernel data in a file system manner. Users and applications can get system information through proc, and can change certain parameters of the kernel. Because the information of the system, such as the process, is dynamically changed, so when the user or application reads the proc file, the proc file system is dynamically reading the required information from the system kernel and submitting the collection flowchart. /proc/meminfo information is as follows: The indicators to be used are: Memtotal, Memfree,buffers,cached memtotal: Total memory size Memfree: Free memory size buffers and cached: Disk cache size buffers and cached differences: buffers refers to the buffer size used to make a block device, he only records the file system metadata and tracking in-flight Pages.cached is used to buffer files. Buffers is used to store content, permissions, and so on in the directory. and cached directly to the memory of our open files, such as the execution of the two command #man X, you can obviously feel that the second opening is much faster. and buffers at any time in the increase, such as two times after the use of Ls/dev, you will find the second execution faster than the first time. This is the difference between buffers/chached.  2, the following respectively from the operating system angle and application perspective to distinguish between buffers and cached use the free command to see the operating system, buffers and cached are already used (Mem: this line)       1      MemFree=total-used     2      314952= 24946552-24631600  for the application (corresponds to-/+ buffers/cache line)      1      memfree=buffers+ cached+free     2      19536392=152116+19069324+314952    By looking at the instructions for the free command, you can see that the value of the command is re-read from the/proc/meminfo file. View the free source package to view its source code, clearly know the source of each of the values (specific content can see the Linux command Free source interpretation: Procps free.c). Sometimes we read the echo of the free command when we calculate memory usage, but sometimes we also read the contents of the file/proc/meminfo directly, after all, the echo data of the free command is obtained from the Meminfo file.   However, due to different Linux distributions, part of the source code will be modified during the system production process. General systems (such as Debian) use the free command and read the Meminfo file in two ways to calculate the same memory utilization. However, for some systems, such as SuSE (which is not sure of each version, this refers to SuSE Enterprise Server 11), in the result of the free command echo, the value of the cached part is not equal to the value displayed by cached in the Meminfo file. Instead, it equals the sum of the cached and sreclaimable portions of the Meminfo file.   That is, Debian-like systems think that:      available memory =free memory +cached memory +buffers memory   and SUSE-like systems think:       available memory =free memory +cached memory +buffers memory +sreclaimable memory  ps: What is sreclaimable? There are many small objects in the Linux kernel that are destroyed very often, such as i-node,dentry. Then these objects if each time the construction of the memory to a page, and its actual size may be only a few bytes, so it is very wasteful, in order to solve this problem introduced a new mechanism to deal with in the same page box how to allocate small memory area, this mechanism can reduce the application and release of memory consumption, The memory of these small memory areas is called slab. The size of the slab is identified in the Meminfo file, and sreclaimable refers to the size of the recoverable slab.

Calculate Linux system memory usage correctly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.