Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux memory management     - Ubuntu 14.04 / Linux Mint 17 How to install the MintMenu 5.5.2 menu (Linux)

- To teach you how to safely send mail (Linux)

- Subsequent binary search tree traversal sequence (Programming)

- C language macro definition #define Usage (Programming)

- To learn linux security (Linux)

- Workaround CentOS error message during compilation PHP5 common (Linux)

- Linux Bash share tips for getting started (Linux)

- CentOS 6 rapid deployment of Java applications (Linux)

- A summary of Java multi-threaded programming - acquaintance multithreading (Programming)

- Install and configure GO 1.2.1 under CentOS 6.5 (Linux)

- Android Application Development: Genymotion can not start solving (Linux)

- Nginx Load Balancing (standby) + Keepalived (Server)

- How Mutt mail client to use cipher text password (Linux)

- Repair Chrome for Linux is (Linux)

- Use smartmontools view the health status of hard disk (Linux)

- 64-bit Ubuntu 15.10 How to compile the latest version of the 32 Wine 1.7.53 (Linux)

- Nginx Beginner Guide (Server)

- MySQL 5.6 master-slave replication configuration (Database)

- Depth understanding of C language (Programming)

- Using Linux strace command trace / debug a program commonly used options (Linux)

 
         
  Linux memory management
     
  Add Date : 2018-11-21      
         
         
         
  Now most of them are running on Linux servers above, therefore, it is necessary as a programmer simply look at how the system is running. For the portion of memory need to know:

Address Mapping
Mode memory management
Missing page exception
First look at some basic knowledge in the process of view, memory is divided into kernel mode and user mode in two parts, the classic proportions as follows:

Usually through the system call from user mode to kernel mode, an interrupt to achieve. User mode memory is divided into different areas for different purposes

Of course, kernel mode and will not indiscriminately use

Here's take a closer look at how the memory is managed.

address

During the mapping of the internal address of Linux logical address -> linear address -> physical address, the physical address of the simplest: the address bus transmitted digital signal, and the linear and logical addresses is represented by a conversion rule, linear address rules are as follows

This section is completed by the MMU, which relates to the main register has CR0, CR3. Machine instructions that appear logical address is a logical address rules are as follows

In Linux logical address is equal to the linear address that is compatible with Inter in order to make things so complicated, Linux simplifies the way a lazy.

Mode memory management

In the system boot time will be to detect the size and circumstances of memory, prior to the establishment of complex structures, we need a simple way to manage memory, which is bootmem, in short, bitmap, but which also has some optimization ideas .

bootmem again how optimization, efficiency is not high, when you want to allocate memory, after all, is going to traverse, buddy system just to solve this problem: to save some within the power of the size of the free memory segments 2, if you want to assign 3page, go 4page taken inside a list, then assign the remaining three one back, just the memory release process is a reverse process.


0,4,5,6,7 are being used can be seen, then, when 1 is released, they will merge them?

staticinlineunsignedlong
__find_buddy_index (unsignedlong page_idx, unsignedint order) {return page_idx ^ (1 << order); // Update the highest bit from 0 to 1 exchange}
You can see from this code, is 0,1 buddy, 2,3 is buddy, 1,2 although adjacent, but they are not. Memory fragmentation is the enemy of the system is running, buddy system mechanism prevents debris ~~ addition to a certain extent, we can get to the number of pages in each order free by cat / proc / buddyinfo.

Each partner system memory is allocated in pages (4KB) for the unit, but when the system is running most of the data structures are very small, as a small object allocation 4KB obviously uneconomical. Linux distribution used to solve small slab object


At runtime, slab to buddy "wholesale" some memory after processing dice "bulk sale" Get out. With the extensive application of large-scale multi-processor systems and NUMA systems, slab finally exposed the shortcomings:

Sophisticated queue management
Queue management and greater data storage overhead
Long-running partial queues can be very long
Support for NUMA very complex
To address these experts have developed a slub: Transformation page structure to reduce management overhead slab structure, each CPU has a local activity slab (kmem_cache_cpu) and the like. For small embedded systems there is a slab emulation layer slob, in such a system it is an advantage.

Small memory problems be solved, but there is a large memory problems: distribution of 10 x 4KB data buddy system, it will go to 16 x 4KB free list go inside (thus obtained is contiguous physical memory), but there are likely to system memory, but the buddy system allocation does not come out, because they are divided into small fragments. So, vmalloc is to use these fragments to piece together a large memory, equivalent to collect some "bits and pieces", assembled into finished products after a "for sale"

Before the memory is directly mapped for the first time feel the presence of p-type management: D In addition to the high-end memory, provides a method for the page kmap assigned a linear address.

Process by the segments of different lengths: the code segment, dynamic library code, global variables and dynamically generated data heap, stack, etc., in managing a Linux virtual address space for each process

We write code malloc finished later, did not immediately take up so much of the physical memory, but only to maintain the above virtual address space only, and only when you actually need to allocate physical memory, which is COW (COPY-ON-WRITE: copy-on-write) technology, and physical distribution process is the most complex part of the missing page exception handling, let's look!

Missing page exception

Prior to the actual needs of data in a virtual memory area mapping relationship between physical memory and will not be established. If part of the process virtual address space access is not yet associated with a page frame, the processor automatically lead to a missing page exception. When kernel handles missing page exception can get the following information:

cr2: access to linear address
err_code: when an exception occurs by the control unit onto the stack, indicating the cause of the abnormal
regs: An exception occurs when the value of the register

Missing page occurs when abnormal, it may be because of the less frequently used the swap to disk, swap-related commands are as follows:

Command Function
swapon open swap
swapoff turn off swap
/ Proc / sys / vm / swappiness score bigger and more active use of swap, you can modify /etc/sysctl.conf add vm.swappiness = xx to modify
If mmap memory is mapped into memory, then read and write the corresponding memory time will produce a page fault exception.
     
         
         
         
  More:      
 
- How to create SWAP files in Ubuntu 14.04 (Linux)
- A process of how to get the current traffic in GNU Linux (Linux)
- Linux performance monitoring - CPU, Memory, IO, Network (Linux)
- About Samba certification process and permissions (Linux)
- How to modify the Emacs Major Mode Shortcuts (Linux)
- Port is not being used, how will bind failure? (Server)
- iOS persistence of data archiving NSKeyedArchiver (Programming)
- Java memory area (Programming)
- Use cmake to compile and install MySQL 5.5 (Database)
- Java Timer (regular calling, to achieve a fixed time to perform) (Programming)
- Linux serial port driver test (Linux)
- Build Golang development environment configuration on Ubuntu 14.04 (Linux)
- Java implementation file encryption and decryption (Programming)
- Linux, modify / retrieve the root password (Linux)
- Ubuntu 14.04 solved using MyEclipse 10.7 flash back (Linux)
- RHEL5 stalled due to power service error system can not start (Linux)
- CentOS ClamAV antivirus package updates (Linux)
- ActiveMQ configuration Getting Started Tutorial (Server)
- Java garbage collection (Programming)
- C ++ based foundation: the difference between C and C ++ (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.