Linux memory and manual release mechanism and swap memory

Today we talk about Linux memory mechanism.

First we look at the concept of reason

First, what is the linux memory mechanisms?

We know, read and write data directly from physical memory than from a hard disk read and write data much faster, therefore, we want to read and write all data are completed in memory, and memory is limited, so that leads to physical the concept of memory and virtual memory.

Physical memory is the system hardware to provide memory size, is a real memory, relative to the physical memory in linux there is a concept of virtual memory, virtual memory is the physical memory strategies in order to meet the proposed, it is the use of disk space a virtualized logical memory, disk space for virtual memory swap space is called (swap space).

As an extension of physical memory, linux will be in the physical memory, virtual memory swap partition, more detailed, is that the kernel will temporarily unused memory block information is written to swap space, so since the physical memory has been released, this memory can be used for other purposes, when the need to use the original content, and this information is re-read into physical memory from the swap space.

Linux memory management page access mechanism is taken, in order to ensure the physical memory can be fully utilized, kernel data block at the appropriate time in the physical memory is not often used in automatic switching to the virtual memory, and the frequently used information reserved to physical memory.

To learn more about linux operating mechanism of memory, we need to know several aspects mentioned below:

  1. Linux system from time to time for page exchange operations, in order to keep as much free physical memory, and even if nothing needs memory, Linux will swap out memory pages temporarily unused. This avoids waiting time required for the exchange.

  2. Linux for page exchange is conditional, not all pages when not in use are switched to virtual memory, linux kernel based on "most often recently used" algorithm, simply swap some pages are not frequently used files into virtual memory, sometimes we'll see to such a phenomenon: linux there are a lot of physical memory, but also use a lot of swap space. In fact, it is not surprising, for example, take up a lot of memory process is running, it takes a lot of memory resources, then there will be some of the less frequently used files are swapped pages into virtual memory, but later this take up a lot of memory resources At the end of the process and releases a lot of memory, it has been swapped out page files are not automatically exchanged into physical memory, unless this is necessary, then the system is at the moment a lot of physical memory will be idle, and swap space are being used, there have been just mentioned phenomenon. On this point, do not worry about anything, as long as know how it is on it.

  3. Page swap space when in use will first be exchanged into physical memory, if at this time there is not enough physical memory to accommodate these pages, they will be immediately swapped out, so since there may not be enough virtual memory space to store these exchanges page will eventually lead to false crash linux, service exceptions and other issues, although linux can restore itself over time, but the system after the recovery has been basically unusable.

Therefore, rational planning and design of Linux memory usage, it is very important.

In the Linux operating system, when the application needs to read the data file, the operating system to allocate some memory, the data will be read from disk into the memory, and then distribute the application data; when you need to file when writing data, the operating system allocates memory to receive user data, then data from memory to disk. However, if there is a lot of data to be read from disk into memory or written to disk by the memory read and write performance of the system becomes very low, because both the data from the disk read or write data to disk, is a very time-consuming and resource-intensive process, in this case, Linux introduced the buffers and cached mechanisms.

buffers and cached memory operations are used once opened files and file attribute information storage systems, so that when the operating system needs to read certain files will first look at buffers and cached memory area, if found, direct readout passed to the application, if required data is found, it is read from disk, which is the operating system's caching mechanism, by caching, greatly improving the performance of the operating system. But the contents of buffers and cached buffer is different.

buffers are used to buffer the piece of equipment to do, it records the file system metadata (Metadata) and tracking in-flight pages, the cached file is used to make the buffer. More popular thing to say: buffers mainly used to store directory what content, attributes, and file permissions and so on. The cached files and programs used to directly opened our memories.

In order to verify our conclusions are correct, vi can be opened by a very large file, see the changes cached, and then vi this file again, feel the similarities and differences between the two open rate is not significantly speed the second time to open faster than the first time it?
Then execute the following command:

find /* -name  *.conf
 

To see whether changes in the value of buffers, and then repeat the find command to see if the display twice the speed of any different.

 

 

Two, linux when to start using virtual memory (swap)?

A.[root@wenwen ~]# cat /proc/sys/vm/swappiness

60

the swappiness 

This Control IS Used to DEFINE How Aggressive The Kernel Will the swap 

Memory Pages. Higher values Will Increase aggressiveness, Lower values 

Decrease The AMOUNT of the swap. A value of 0 instructs The Kernel Not to 

Initiate the swap an until The AMOUNT of Free and File-backed iS less pages and the 

Within last at the High Water Mark in a Zone. 

at the iS 60. the default value 

translates 

this parameter is aggressive (aggressive) custom kernel swap memory pages. Greater value will increase aggressiveness, lower values reduce the amount of the swap. A value of 0 will instruct the kernel not to use swap, only when the number of free memory pages and files that are used less than one zone of high water, will use the swap. 

The default value is 60. 

About aggressive here to see is foggy. This value is probably only know the meaning. In some environments, users have been complaining about why so many Swap usage, obviously there are a lot of available memory, but in fact this is a normal phenomenon.

  

swappiness = 0 indicates when the maximum physical memory and swap space is,

swappiness = 100 indicates when an active use of swap area, and the timely data transfer memory to the swap space on the inside.

usually:

swap area setting proposal is twice the memory (memory 4G is less than or equal to), if the memory is greater than 4G, swap memory is large than long line. Also try swappiness will reduce the performance of such a system would be better.

B. modify parameters swappiness

Temporary fixes:

[root@wenwen ~]# sysctl vm.swappiness=10

vm.swappiness = 10

[root@wenwen ~]# cat /proc/sys/vm/swappiness                 

10

 

Permanent modifications:

[root@wenwen ~]# vim /etc/sysctl.conf

Join parameters:

vm.swappiness = 35

Then in direct:

[root@wenwen ~]# sysctl -p

Check whether the effect:

cat /proc/sys/vm/swappiness

35

 

 

 

Third, how to release memory?

General system memory is not automatically released

The key configuration file / proc / sys / vm / drop_caches . This document records the release of cache parameters, the default value is 0, that is, do not release the cache. His value can be any number between 0 to 3, representing different meanings:

0 - do not release
1-- released page cache
2 - release dentries and inodes
3 - Release all caches

Practical operation:

 

wKiom1kIfqfCWjMwAABa_PWXhZ8253.png-wh_50

Obviously a lot of extra free memory of it

 

 

Fourth, how the release of swap?

Premise: First, to ensure remaining to be greater than or equal swap memory usage, otherwise they will be down! According to the mechanism of memory, swap partition Once released, all files stored in the swap partition will dump physical memory. Usually by re-mount the swap partition complete release swap.

a. View the current swap partition is mounted in what?

wKioL1kIg_-QgRPNAAAc0YY8hDs713.png-wh_50

b. to shut down the partition

wKiom1kIhCjzKPz0AAAJ4nqUEqM343.png-wh_50

. C Check status:

wKioL1kIhEuQnJNsAAARrgqCj1I720.png-wh_50

d. View the swap partition is shut down, the bottom line shows the full 0

wKioL1kIhGPAeVG_AAAmRTAqcso981.png-wh_50

e. Move the swap mount / dev / sda5

wKiom1kIhL-DWuyLAAAJq9dwCWk401.png-wh_50

f. Check mount successful

wKioL1kIhJKAttdNAAAbWQ1IwjE929.png-wh_50

 

 

Fifth, the small number of actual examples?

I set the mysql time, will

my.cnf file

innodb_buffer_pool_size = 6G (my operating system's memory is 6G, generally this value is set to 80% of the operating system memory)

This value is not the better settings. Set too large, it will lead to the system swap space is occupied, causing the operating system slows down, thereby reducing the efficiency of the sql query.

Here you can understand, when I set this buffer_pool_size too large, with the operating system memory as large as when I use mysql, calls a lot of data into memory for some time, due to the linux memory mechanism, according to a recent re the principle of optimal, part of the data filtering into the swap partition, swap partition is not timely clean out the data, other data when the call again, and screening will be part of the data into the swap partition, there may be a phenomenon, that is, there is a surplus of physical memory, even empty, and the swap partition is full.

Guess you like

Origin www.cnblogs.com/robinunix/p/11248878.html