Linux basics-Memory Management Mechanism

Source: Internet
Author: User

1 Main Features of Linux memory management

--------------------------------------------------------------------------------
Regardless of the physical memory size, Linux fully utilizes it to read the hard disk data called by some programs into the memory, and improve the data access performance of the Linux system by using the high-speed features of memory read/write. In Windows, memory is allocated to applications only when memory is needed, and large memory space cannot be fully utilized. This feature of Linux mainly uses idle physical memory to divide a part of space as cache and buffers to improve data access performance. Page cache is a major disk cache implemented by Linux kernel. It is mainly used to reduce disk I/O operations. Specifically, by caching data in the disk to the physical memory, you can change access to the disk to access the physical memory.

2 physical memory and virtual memory

--------------------------------------------------------------------------------
Physical memory is the memory size provided by the system hardware and is the real memory. Compared with physical memory, there is also a concept of virtual memory in Linux, virtual Memory is a strategy proposed to meet the shortage of physical memory. It is a logical memory virtualized by the disk Space. The disk Space used as the virtual memory is called the Swap Space ).
Linux memory management adopts a paging access mechanism. To ensure that the physical memory can be fully utilized, when appropriate, the kernel automatically exchanges infrequently used data blocks in the physical memory to the virtual memory, and retains frequently used information to the physical memory.


Linux memory running mechanism:
2.1 The Linux system will switch pages from time to keep as much free physical memory as possible
2.2 In Linux, page switching is conditional. Not all pages are switched to the virtual memory when they are not in use. The Linux kernel uses the "most recently used" algorithm, only swap some infrequently used page files to the virtual memory
2.3 swap space pages will be first exchanged to the physical memory during use. If there is not enough physical memory to accommodate these pages, they will be immediately exchanged


3 Linux memory monitoring

--------------------------------------------------------------------------------
Free-m:
[Root @ rango backup_CentOS] # free-m
Total used free shared buffers cached
Mem: 3805 3683 121 0 120 690
-/+ Buffers/cache: 2872 932
Swap: 2041 134 1907
Note:
Total: total physical memory size used: used physical memory size free: idle physical memory size
Shared: memory size shared by multiple processes buffers/cached: disk cache size
Mem: indicates the physical memory usage (-/+ buffers/cached): indicates the disk cache usage status.
Swap: indicates the memory usage status of the Swap space.
(-Buffers/cache) used memory size: 2872 MB (refers to used-buffers-cached in the first part of the Mem row)
(+ Buffers/cache) free memory: 932 MB (refers to the first part of the Mem row free + buffers + cached)
Memory available from the kernel perspective: 121 MB
Memory available from the application: free + buffers/cache = 121 + 120 + 690 = 931 M, which is the free value of the second row. 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.


Linux cache mechanism: buffers and cached are both in-memory operations to save files that have been opened by the system and file attribute information. In this way, when the operating system needs to read some files, it will first be searched in the buffers and cached memory areas. If it is found, it will be directly read and transmitted to the application. If it does not find the data needed, it will be read from the disk.
Buffers is used to buffer Block devices. It only records metadata (metadata) and tracking in-flightpages of the file system, while cached is used to buffer files. More broadly speaking, buffers is mainly used to store contents in directories, file attributes, and permissions. Cached is used directly to remember the files and programs we opened.


4 Linux memory release process

--------------------------------------------------------------------------------
4.1 free-m view memory usage


4.2 sync: Use the sync command to ensure the integrity of the file system. The sync command runs the sync subroutine to write all unwritten system buffers to the disk, contains modified I-node, delayed block I/O, and read/write ing files. To ensure reliability, you should execute the sync command twice because the information is not actually written to the disk when the sync command is complete.


4.3 Modify/proc/sys/vm/drop_caches:
Echo 3>/proc/sys/vm/drop_caches
Note:
1)/proc is a virtual file system. We can use its read/write operations as a means of communication with the kernel object. In other words, you can modify the file in/proc to adjust the current kernel behavior. In other words, we can adjust/proc/sys/vm/drop_caches to release the memory.
2) drop_caches:
Writing to this file causes the kernel to drop cleancaches, dentries and inodes from memory, causing that memory tobecomefree.
To free pagecache, use echo 1>/proc/sys/vm/drop_caches;
To free dentries and inodes, use echo 2>/proc/sys/vm/drop_caches;
To free pagecache, dentries and inodes, use echo 3>/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects arenot freeable, the user shoshould run syncfirst.

  • 1
  • 2
  • Next Page

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.