Linux memory management-free command

Source: Internet
Author: User

Linux memory management-free command 1 free memory management as a linux system administrator, it is very important to monitor the memory usage status, through monitoring to help you understand the memory usage status, for example, whether the memory usage is normal, whether the memory is in short supply, and so on. The most commonly used commands for monitoring memory include free and top. below is the output of a system free: [] # free total used free shared buffers cachedMem: 16402432 16360492 41940 0 465404 12714880-/+ buffers/cache: 3180208 13222224 8193108 Swap: 264 8192844 row: Average total: the total size of the physical memory. Memory used: how small the physical memory is. Idle free: idle physical memory value. Memory shared: memory value shared by multiple processes. Cached buffers/cached: disk cache size. Row 2: physical memory usage. Row 3 (-/+ buffers/cached): indicates the disk cache usage status. Row 4: Swap indicates the memory usage status of the Swap space. The memory status output by the free command can be viewed from two perspectives: one is from the kernel perspective and the other is from the application layer perspective. 1. from the kernel point of view, the memory status is that the kernel can be directly allocated to it. No additional operation is required, that is, the value of the Mem entry in the second line of the free command output above. We can see that, the physical memory of this system has 16 GB, And the idle memory is only 41940 kb, that is, a little more than 40 MB. Let's do this calculation: 16402432-16360492 = 41940 is actually the total physical memory minus the used physical memory to get the free physical memory size. [Note: The available memory value 41940 does not include the memory size in the buffers and cached States] [NOTE: If you think the system's idle memory is too small, you will be wrong! In fact, the kernel fully controls the memory usage. linux will change the memory in the buffers and cached state to the free State when the memory is needed or when the system is running gradually, for use by the system.] 2. from the application layer perspective, the system memory usage status is the memory size that can be used by applications running on linux, that is, the third line of the free command "(-/+ buffers/cached) "output, we can see that the memory used by the system is only 3180208 K, and the idle memory reaches 13222224 K, continue to do this calculation: 41940 (Men: free) + (465404 (Men: buffers) + 12714880 (Men: cached) = 13222224 (-/+ buffers/cached: free) through this equation, the physical memory value available for the application is the sum of the free values of the Mem items plus the buffers and cached values. That is to say, the free value includes the size of the buffers and cached items. For the application, the memory occupied by buffers/cached is available, because buffers/cached is used to improve the file reading performance. When the application needs to use the memory, buffers/ca Ched will soon be recycled for use by the Supply Application. 3. the similarities and differences between buffers and cached are in the Linux operating system. When an application needs to read data from a file, the operating system first allocates some memory to read data from the disk into the memory, then, the data is distributed to the application. When you need to write data to a file, the operating system first allocates memory to receive user data, and then writes the data from the memory to the disk. However, if a large amount of data needs to be read from the disk to the memory or written into the disk by the memory, the system's read/write performance becomes very low, because whether it is reading data from the disk, writing data to a disk is a very time-consuming and resource-consuming process. In this case, the buffers and cached mechanisms are introduced in linux. Both buffers and cached are in-memory operations to save the files that have been opened by the system and the file attribute information. In this way, when the operating system needs to read some files, it will first search in the buffers and cached memory areas, if the data is found, read the data directly and send it to the application. If the data is not found, the data is read from the disk. This is the operating system cache mechanism, which greatly improves the operating system performance through caching. However, the buffer content of buffers and cached is different. Buffers is used to buffer Block devices. It only records metadata (metadata) and tracking in-flight pages of the file system, while cached is used to buffer files. [More broadly speaking, buffers is mainly used to store the attributes and permissions of files in the directory. Cached is used directly to remember the files and programs we opened.] Test: to verify whether our conclusion is correct, you can open a very large file through vi, check the changes in cached, and then vi the file again, what are the differences between the two open speeds? Is the second open speed faster than the first one? Run the following command: find/*-name *. conf to check whether the buffers value has changed. Then, run the find command again to check whether the display speed is different. The memory operating principle of the Linux operating system is designed to a large extent based on the requirements of the server. For example, the system buffer mechanism caches frequently used files and data in cached, linux always tries to cache more data and information, so that you can directly retrieve the data from the memory when you need it again, without a long disk operation, this design improves the overall performance of the system. Although the use of swap in the swap space has become very cheap, swap still has great value for use. Reasonable planning and use of swap partitions are crucial to the stable operation of the system. In Linux, you can use a regular file in the file system or an independent partition as a swap space. At the same time, linux allows multiple swap partitions or swap files. 1. the swap file required to create a swap space is a common file. However, unlike creating a swap space, the swap file must be created using the dd command, at the same time, this file must be located on the local hard disk and cannot be used to create swap files on the Network File System (NFS. For example: [root @ localhost ~] # Dd if =/dev/zero of =/data/swapfile bs = 1024 count = 6553665536 + 0 records in65536 + 0 records out to create a swap file with continuous space, the size is about 60 MB. The dd command is described as follows: if = input file or device name. Of = Name of the output file or device. Ibs = bytes indicates that bytes are read at a time (that is, the size of a block is bytes ). Obs = bytes indicates writing bytes at a time (that is, the size of a block is bytes ). Bs = bytes, and set the size of read/write blocks, in bytes. This parameter can replace ibs and obs. Count = blocks only copies blocks. Skip = blocks indicates that the blocks are skipped from the beginning of the input file and then copied. Seek = blocks indicates that the blocks are skipped from the beginning of the output file and then copied. (Usually only valid when the output file is a disk or tape) the input device/dev/zero here indicates a device file whose output is always 0, you can use it as an input to obtain all files that are empty. 2. Activate and use swap. First, use the mkswap command to specify the device or file used as the swap space: [root @ localhost ~] # Mkswap/data/swapfileSetting up swapspace version 1, size = 67104 kB [root @ localhost backup] # free total used free shared buffers cachedMem: 2066632 1998188 68444 0 26160-/+ buffers/cache: 1588044 383984 Swap: 1682648 4088500 101036 from the above output, we specify a 3987464 kB Swap space, the new swap space is not activated yet. The following describes the mkswap command. Generally, mkswap uses the format mkswap: mkswap [parameter] [device name or file] [swap zone size] parameter:-c: Before the swap zone is created, check whether any damaged block exists first. -V0: creates an old-style swap zone, which is the default value. -V1: Create a new exchange zone. Swap size: the size of the SWAp, in 1024 bytes. After setting the swap partition, run the swapon command to activate swap: [root @ localhost ~] #/Usr/sbin/swapon/data/swapfile [root @ localhost backup] # free total used free shared buffers cachedMem: 2066632 1997668 68964 0 27404-/+ buffers/cache: 381384 1685248 Swap: 4154028 100976 4053052 the free command shows that the swap size has changed from 4088500k to 4154028 k, and the difference is about 60 m, which is equal to the size of an added Swap file, this indicates that the new swap partition is ready for use. However, if linux is restarted, the newly added swap space becomes unavailable. Therefore, you need to add the automatic loading settings in/etc/fstab:/data/swapfile none swap sw 0 0, after linux restarts, It can automatically load swap partitions. In fact, linux will execute the "swapon-a" command during startup. This command will load all the swap spaces listed in/etc/fstab. 3. Remove swap and use swapoff to remove a swap space [root @ localhost ~] #/Usr/sbin/swapoff/data/swapfile you can also use "swapoff-a" to remove all swap spaces defined in/etc/fstab, "swapoff-a" corresponds to the "swapon-a" mentioned above. After "swapoff-a" is executed, the free command output is as follows: [root @ localhost backup] # free total used free shared buffers cachedMem: 2066632 2048724 17908 0 30352-/+ buffers/cache: 1642748 375624 Swap: 0 0 0

Related Article

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: 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.