Linux memory mechanism and manual release of Swap and memory

Source: Internet
Author: User

Wow, it's been a long time since it's been updated, let's talk about Linux's memory mechanism today.

First, let's take a look at the concept

First, what is the memory mechanism of Linux?

We know that reading and writing data directly from physical memory is much faster than reading and writing data from a hard disk, so we want all of the data read and written to be done in memory, and memory is limited, which leads to the concept of physical and virtual memory.

Physical memory is the amount of memory provided by the system hardware, is the real memory, relative to the physical memory, under Linux there is a virtual memory concept, virtual memory is to meet the lack of physical memory of the proposed strategy, it is the use of disk space virtual out of a piece of logical memory, The disk space used as virtual memory is called swap space.

As an extension of physical memory, Linux will use the virtual memory of the swap partition when physical memory is low, in more detail, the kernel will write the memory block information that is temporarily unused to the swap space, so that the physical memory has been released, this memory can be used for other purposes, when the original content needs to be used, This information is re-read into the physical memory from the swap space.

Linux memory management is a paging access mechanism, in order to ensure that the physical memory can be fully utilized, the kernel will be in the physical memory of infrequently used data blocks automatically swapped into virtual memory, and the information often used to retain the physical memory.

To learn more about Linux memory operating mechanisms, you need to know the following:

  1. The Linux system will occasionally make a paging operation to keep as much free physical memory as possible, even if there is nothing to do with memory, and Linux will swap out the memory pages that are temporarily unused. This avoids the time required to wait for the interchange.

  2. Linux for the page exchange is conditional, not all the pages are swapped to virtual memory when not in use, the Linux kernel based on the "most Frequently used" algorithm, only some infrequently used paging files to virtual memory, sometimes we will see a phenomenon: Linux physical memory is still many, But the swap space is also used a lot. In fact, this is not surprising, for example, a very large memory of the process to run, the need to spend a lot of memory resources, there will be some infrequently used paging file is swapped into virtual memory, but later this memory-intensive process ended and freed a lot of memory, the page just swapped out The file is not automatically swapped into physical memory, unless it is necessary, then the system physical memory will be much more idle, while the swap space is also being used, there is a phenomenon just said. Don't worry about that, just know what's going on.

  3. The swap space pages are swapped to physical memory first, and if there is not enough physical memory to accommodate them, they will be swapped out immediately, so there may not be enough space in the virtual memory to store the swap pages, which can eventually lead to a fake crash, service exception, etc. While Linux can recover itself over time, the restored system is largely unusable.

Therefore, it is very important to plan and design the use of Linux memory rationally.

In the Linux operating system, when the application needs to read the data in the file, the operating system allocates some memory, reads the data from the disk into the memory, and then distributes the data to the application, and when the data needs to be written to the file, the operating system allocates the memory to receive the user data first. The data is then written from memory to disk. However, if there is a large amount of data that needs to be read from disk to memory or written to disk by memory, the system's read and write performance becomes very low, since it is a time-consuming and resource-intensive process to read data from disk or write data to disk, in which case Linux introduces buffers and Cached mechanism.

Buffers and cached are memory operations that are used to save files and file attribute information that have been opened by the system, so that when the operating system needs to read some files, it will first look in the buffers and cached memory areas, and if found, read them directly to the application. If you do not find the data needed to read from disk, this is the operating system caching mechanism, through the cache, greatly improve the performance of the operating system. But the content of buffers and cached buffer is different.

buffers is used to buffer the block device, it only records the file system metadata (metadata) and tracking in-flight pages, and cached is used to buffer the file. More commonly said: buffers is mainly used to store content in the directory, file attributes and permissions and so on. and cached is used directly to memorize the files and programs we have opened.

In order to verify our conclusion is correct, you can open a very large file by VI, look at the change of cached, and then again VI this file, feel how the speed of two times to open the similarities and differences, is not the second opening speed significantly faster than the first time?
Then execute the following command:

Find/*-name *.conf

See if the value of the buffers changes, and then repeat the Find command to see how the two times the display speed is different.



Second, when does Linux start using virtual memory (swap)?

A.[[email protected] ~]# cat/proc/sys/vm/swappiness

60

The above 60 means that the physical memory will only use swap when 60% is used.

Swappiness=0 indicates the maximum use of physical memory, then the swap space,

SWAPPINESS=100 indicates active use of the swap partition, and the memory data is moved into the swap space in a timely manner.

Typically:

The swap partition setting is recommended to be twice times the memory (memory is less than or equal to 4G), if the memory is greater than 4g,swap as long as the memory is larger than the line. In addition, try to reduce the swappiness, so that the performance of the system will be better.

B. Modifying the swappiness parameter

Temporary modification:

[Email protected] ~]# sysctl vm.swappiness=10

Vm.swappiness = 10

[Email protected] ~]# cat/proc/sys/vm/swappiness

10


Permanent modification:

[Email protected] ~]# vim/etc/sysctl.conf

Add parameter:

Vm.swappiness = 35

Then in direct:

[Email protected] ~]# sysctl-p

See if it takes effect:

Cat/proc/sys/vm/swappiness

35




Third, how to free memory?

The general system does not automatically release memory

Critical configuration file/proc/sys/vm/drop_caches. This file records the parameters of the cache release, the default value is 0, which is not to release the cache. His value can be any number between 0~3, which represents a different meaning:

0– not release
-Free Page Cache
Release Dentries and Inodes
3– Release All Caches

Real:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/93/28/wKiom1kIfqfCWjMwAABa_PWXhZ8253.png-wh_500x0-wm_ 3-wmp_4-s_3766427302.png "title=" Z.png "alt=" Wkiom1kifqfcwjmwaaba_pwxhz8253.png-wh_50 "/>

It's obvious that there's a lot more free memory out there.



Iv. How to release swap?

Premise: first to ensure that the memory surplus is greater than or equal to the swap usage, otherwise it will go down! Depending on the memory mechanism, once the swap partition is released, all files stored in the swap partition are dumped into physical memory. Swap is usually done by re-mounting the swap partition.

A. See where the current swap partition is mounted?

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/93/27/wKioL1kIg_-QgRPNAAAc0YY8hDs713.png-wh_500x0-wm_ 3-wmp_4-s_3406965261.png "title=" C.png "alt=" Wkiol1kig_-qgrpnaaac0yy8hds713.png-wh_50 "/>

B. Shut down this partition

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/93/29/wKiom1kIhCjzKPz0AAAJ4nqUEqM343.png-wh_500x0-wm_ 3-wmp_4-s_3264097685.png "title=" D.png "alt=" Wkiom1kihcjzkpz0aaaj4nqueqm343.png-wh_50 "/>

C. View status:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/93/27/wKioL1kIhEuQnJNsAAARrgqCj1I720.png-wh_500x0-wm_ 3-wmp_4-s_1232276929.png "title=" E.png "alt=" Wkiol1kiheuqnjnsaaarrgqcj1i720.png-wh_50 "/>

D. see if the swap partition is off, the bottom line shows full 0650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/93/27/wKioL1kIhGPAeVG_ Aaamrtaqcso981.png-wh_500x0-wm_3-wmp_4-s_364449478.png "title=" F.png "alt=" Wkiol1kihgpaevg_aaamrtaqcso981.png-wh _50 "/>

E. Attach swap to/dev/sda5

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/93/29/wKiom1kIhL-DWuyLAAAJq9dwCWk401.png-wh_500x0-wm_ 3-wmp_4-s_1562435597.png "title=" G.png "alt=" Wkiom1kihl-dwuylaaajq9dwcwk401.png-wh_50 "/>

F. See if the mount is successful

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/93/27/wKioL1kIhJKAttdNAAAbWQ1IwjE929.png-wh_500x0-wm_ 3-wmp_4-s_4130644756.png "title=" H.png "alt=" Wkiol1kihjkattdnaaabwq1iwje929.png-wh_50 "/>








This article is from the "Flying Small GUI" blog, make sure to keep this source http://9237101.blog.51cto.com/9227101/1921260

Linux memory mechanism and manual release of Swap and memory

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.