Linux memory allocation mechanism--partner system and slab

Source: Internet
Author: User

one of the important things about kernel memory management is how to avoid fragmentation when you frequently request to release memory. This requires the kernel to adopt a flexible and appropriate memory allocation strategy. Typically, memory allocations generally have two situations: large objects (large contiguous space allocations), small objects (small spatial allocations). For different requirements, Linux has taken the partner system algorithm and slab for memory allocation respectively.

Partner system: Divides all the free page frames into 11 block lists, each of which is a page box block of size 1,2,4,8,16,32,64,128,256,512 and 1024 contiguous page frames, respectively. The largest page box block contains 1024 contiguous page boxes, corresponding to 4MB-size contiguous memory. Suppose you want to apply for a block of 256 page boxes, then find the free block from the linked list of 256 contiguous page boxes in the node, and if not, go to the list of 512 page boxes and find the block that divides the page box block into 2 256 page boxes, one assigned to the application, The other one moves to a list of 256 page boxes. If there are still no free blocks in the list of 512 page boxes, continue to find-split-assign and transfer to the list of 1024 page boxes. If it still does not, an error is returned. The Used page box block, when released, actively merges two contiguous page box blocks into a larger page box block, which is then inserted into the linked list of the corresponding specification as a node.

The partner system solves the problem of external fragmentation (fragmentation between page frames) well:

, the maximum number of free page boxes in the current memory segment is 3 pages in a row. If you request 4 contiguous page box-size memory At this point, you can only intercept the larger free memory, and over time, the gap left between these page boxes becomes an external fragment. The partner system manages (allocates, merges) these external fragments so that the in-memory page boxes are used as much as possible.

SLAB: The partner system allocates memory when it is based on a page box, which is larger. What if it is a dozens of-byte small memory allocation? At this point you need to use the slab mechanism. Slab allocators are managed based on objects , so-called objects are data structures in the kernel (for example, task_struct,file_struct, etc.). Objects of the same type are grouped, and each time an object is requested, the slab allocator allocates a unit of that size from a slab list, and when it is released, it is re-saved in the list instead of being returned directly to the partner system, thus avoiding internal fragmentation. The slab allocator does not discard objects that have already been allocated, but frees them and saves them in memory. When slab allocates an object, the memory block of the recently freed object is used, so the probability of its residing in the CPU cache is greatly increased. In other words: maintain a slab list in memory, list items corresponding to the various data structure size of memory block, when a small object request memory, directly from the slab list of object type list items, the corresponding size of the memory allocated; After the object is exhausted, Releases the object and returns the block of memory that the object occupies to the slab list for use by the next object of the same type.

Similarly, because slab is a memory allocation for small objects, it is a good solution to the fragmentation (internal fragmentation) of memory allocations inside the page box.

Linux memory allocation mechanism--partner system and slab

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.