Centos 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. it helps you to understand the memory usage status, such as whether the memory usage is normal and whether the memory is in short supply, 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 cached

Mem: 16402432 16360492 41940 0 465404 12714880

-/+ Buffers/cache: 3180208 13222224

Swap: 8193108 264 8192844

The first line:

Ltotal: the total size of physical memory.

Lused: how small the physical memory is.

Lfree: idle physical memory value.

Lshared: Memory value shared by multiple processes.

Lbuffers/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. view the memory status from the kernel perspective

That is, 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. it can be seen that the physical memory of the system is 16 GB, the idle memory is only 41940 KB, that is, a little more than 40 MB. let's do this calculation:

16402432-16360492 = 41940

In fact, the total physical memory minus the used physical memory is the idle 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. view the system memory usage status from the application layer perspective

That is, the memory size that can be used by applications running on linux, that is, the output of the third line of the free command (-/+ buffers/cached, the memory used by the system is only 3180208 KB, and the idle memory reaches 13222224 KB. continue with this calculation:

41940 (Men: free) + (465404 (Men: buffers) + 12714880 (Men: cached) = 13222224 (-/+ buffers/cached: free)

Through this equation, we can see that the physical memory value available for the application is the free value of the Mem item plus the sum of the buffers and cached values. that is to say, the free value includes the size of the buffers and cached items,

For applications, 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/cached will be quickly recycled for use by the application.

3. similarities and differences between buffers and cached

In Linux, 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 to see the changes in cached, and then vi the file again, I feel the similarities and differences between the two open speeds, is the second open faster than the first one?

Run the following command:

Find/*-name *. conf

Check whether the buffers value has changed, and then execute the find command again to see if the display speed is different twice.

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.

Use of swap space swap

Although the current memory has become very cheap, swap still has great application value. 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. create a swap space

The swap file required for creating a swap space is a common file. However, unlike creating a swap file, the swap file must be created using the dd command, and the file must be located on the local hard disk, you cannot create swap files on the Network File System (NFS. For example:

[Root @ localhost ~] # Dd if =/dev/zero of =/data/swapfile bs = 1024 count = 65536

65536 + 0 records in

65536 + 0 records out

In this way, a swap file with continuous space is created, and the size is about 60 MB. The following is a brief description of the dd command:

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 indicates a device file whose output is always 0. you can use it as the 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/swapfile

Setting up swapspace version 1, size = 67104 kB

[Root @ localhost backup] # free

Total used free shared buffers cached

Mem: 2066632 1998188 68444 0 26160 1588044

-/+ Buffers/cache: 383984 1682648

Swap: 4088500 101036 3987464

The above output shows that a 67104 kB swap space is specified, and the new swap space is not activated yet.

The following describes the mkswap command. the general format of mkswap is:

Mkswap [parameter] [device name or file] [swap zone size]

Parameters:

-C: Check whether damaged blocks exist before creating a swap zone.

-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 cached

Mem: 2066632 1997668 68964 0 27404 1588880

-/+ 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 new swap space becomes unavailable. Therefore, you need to add the automatic loading settings in/etc/fstab:

/Data/swapfile none swap sw 0 0

So far, linux can automatically load swap partitions after restart. 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

You can use swapoff to remove a swap space.

[Root @ localhost ~] #/Usr/sbin/swapoff/data/swapfile

In fact, you can also use "swapoff-a" to remove all swap spaces defined in/etc/fstab. The "swapoff-a" here 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 cached

Mem: 2066632 2048724 17908 0 30352 1642748

-/+ Buffers/cache: 375624 1691008

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