In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "what is the allocation and release of Linux memory". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the allocation and release of Linux memory" can help you solve your doubts.
Understand the memory allocation mechanism (sharing mapping and request paging)
Through the pmap command, you can get the memory information mapped in the logical address space of the user process:
Pmap-x $pid
Where-x means to get more information.
Here is an example:
Pmap-x $(pidof emacs) | head-20
Where "Address" refers to the logical address space of the process.
The "Kbytes" column represents the capacity of the corresponding logical address, in Kb
The "RSS" column represents the actual amount of physical memory used, which is generally less than the "Kbytes" value because of the paging mechanism.
"Mapping" lists the mapping of logical memory, where "[annon]" represents the heap space allocated by the malloc function (anonymous memory), and "[stack]" is the stack space of the process, both of which map physical memory to the process's logical memory. File names such as "emacs-25.3" and "libpixbufloader-svg.so" indicate that they perform file mapping, and they correspond to files on disk. When these files are read into the cache, the corresponding memory space is mapped to the logical memory of the process.
When multiple programs share the same file mapping (shared library), they can share the same space in the disk cache, thus saving the use of physical memory. This technique is called "shared mapping" technology.
In addition to shared libraries, the fork of processes also uses shared mapping technology. When the parent process forks the child process, the Linux kernel does not actually copy the contents in memory, but shares the content mapped to the logical address space of the parent process intact to the logical address space of the child process. However, in order to prevent the memory operations of the parent process and the child process from affecting each other, the corresponding memory area is temporarily set to write protection when the Linux kernel carries out shared mapping. A read-only exception is thrown when a party process attempts to manipulate memory. When the kernel detects this exception, it copies the memory page of the operation so that both processes can write independently. This mechanism of replication on write is called "copy-on-write on write".
On the other hand, when a process reads the contents of an executable file or shared library file into memory and maps it to the process logical address space, it does not read all of the file contents, but first marks "the contents of the file have been mapped into the logical address space". When a process accesses the logical address space, a paging error is thrown because there is no corresponding physical memory. When the content detects the exception, the desired portion of the saved page is read into memory as units. This mechanism of reading only what is needed is called request paging.
Understand the memory release mechanism
When other processes need new physical memory, it involves the problem of how to free or swap out the physical memory that still has data remnants.
When new physical memory is needed, the memory pages recorded in Inactive (file) and Active (file) are released first. You only need to write dirty data to the file and release the memory pages.
Inactive (anon) and Active (anon) memory pages need to be swapped to swap on the physical disk before releasing. Specifically, Linux will make a mark on the process page table to mark the logical address corresponding to swapping out memory. When the process accesses the logical address, it will generate an exception that the corresponding physical memory does not exist. When the Linux kernel detects this exception, it will load the data from swap into free memory again and reconfigure the page table information.
The Linux kernel uses two mechanisms to speed up swapping out processing:
One is pre-reading.
When a memory page needs to be swapped in, the Linux kernel swaps in the next few memory pages together. Because it is highly possible for a process to access multiple memory pages in succession. The number of pre-read pages is determined by the kernel parameter vm.page-cluster to be 2 ^ vm.page-cluster.
The other is the swap cache.
That is, after swapping in a memory page, the original data is still retained in the swap space on the physical disk, and the memory in this state is recorded in the swap cache list. In this way, when you need to swap out the data of the memory page recorded on the swap cache again, there is no need to swap in again.
The memory usage of each process can be viewed by viewing the / proc/ process ID/status
Cat / proc/$ (pidof emacs) / statusName: emacs Umask: 0022 State: s (sleeping) Tgid: 6769 Ngid: 0 Pid: 6769 PPid: 1 TracerPid: 0 Uid: 1000 1000 1000 Gid: 1000 1000 1000 FDSize: 64 Groups: 986,998 1000 NStgid: 6769 NSpid: 6769 NSsid: 6769 VmPeak: 567040 kB VmSize: 567040 kB VmLck: 0 kB VmPin: 0 kB VmHWM: 241176 kB VmRSS: 241176 kB RssAnon: 204544 KB RssFile: 36604 kB RssShmem: 28 kB VmData: 231712 kB VmStk: 1596 kB VmExe: 2332 kB VmLib: 47832 kB VmPTE: 1008 kB VmSwap: 0 kB HugetlbPages: 0 kB CoreDumping: 0 Threads: 4 SigQ: 1/15456 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000004381000 SigCgt: 00000001db816eff CapInh: 0000000000000000 CapPrm: 0000000000000000 CapEff: 0000000000000000 CapBnd: 0000003fffffffff CapAmb: 0000000000000000 NoNewPrivs: 0 Seccomp: 0 Cpus_allowed: 3 Cpus_allowed _ list: 0-1 Mems_allowed: 1 Mems_allowed_list: 0 voluntary_ctxt_switches: 12951 nonvoluntary_ctxt_switches: 21641
Some of the more useful items are:
VmData
Size of the data segment
VmExe
Size of the text segment
VmHWM
The * value of the current physical memory usage
WmLck
Memory size locked with mlock
VmLib
Usage of shared libraries
VmPTE
The size of the page table
VmPeak
The value of the current physical memory
VmRSS
Actual usage of physical memory
VmSize
The size of the logical address
VmStk
Size of the stack
VmSwap
Usage of swap space
After reading this, the article "what is the allocation and release of Linux memory" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.