Slab Memory Management Sharing

Source: Internet
Author: User

1.What isSlab

Slab is a medium memory management algorithm, the first Sun of engineers, mainly based on a factor of consideration:

1: Kernel functions often tend to request the same data type repeatedly. For example, when a process is created, a piece of memory is requested to storemmstructure.

2: Different structures use different allocation methods to improve efficiency. Similarly, if the process is undone, the kernel does notmmThe structure is released, but stored in a buffer, and if requested latermmthe behavior of the storage space can be taken directly from the buffer without the need to reallocate memory.

3:As we have previously analyzed, if the partner system is frequently allocated, releasing memory can affect the efficiency of the system, so that the memory to be released into the buffer, until more than one threshold to release it to the partner system, so as to reduce the pressure of the partner system to a certain extent

4:In order to mitigate the "fragmentation", it is often possible to put small chunks of memory in2are organized in multiples, similar to the partner system

2.slabthe concrete idea of the algorithm

The idea of

slab memory is divided into unequal sizes slabs chunks slabs slab is divided into equal size chunks slab chunk of Background:white " size is unequal ). chunk start from a minimum number in turn, Grows by a factor until the maximum possible value is reached.

  If the minimum value is Span style= "Background:white" >400b 1MB , Factor is 1.20 slab chunk slab1-400b slab2-480b slab3-576b

when assigning an object, select the appropriate object based on its size Slab , in the corresponding Slab assigns an object if the corresponding Slab when there is no free space, then ask the system for a Slab , in the allocation.

when the object is disposed, the object needs to be directly Slab you can set the object in the idle state.

This idea can be used flexibly, the main purpose is to classify it according to the frequency of the memory area, create a set of specific size of the dedicated buffer to deal with, in order to avoid the generation of fragments. For less -used memory areas, you can create a common set of buffers to handle (such as large memory allocations in our code), even if the processing pattern is fragmented, and has little impact on the performance of the system as a whole.

Linux Kernel pair Slab Application of:

1.What isSlab

Slab is a medium memory management algorithm, the first Sun of engineers, mainly based on a factor of consideration:

1: Kernel functions often tend to request the same data type repeatedly. For example, when a process is created, a piece of memory is requested to storemmstructure.

2: Different structures use different allocation methods to improve efficiency. Similarly, if the process is undone, the kernel does notmmThe structure is released, but stored in a buffer, and if requested latermmthe behavior of the storage space can be taken directly from the buffer without the need to reallocate memory.

3:As we have previously analyzed, if the partner system is frequently allocated, releasing memory can affect the efficiency of the system, so that the memory to be released into the buffer, until more than one threshold to release it to the partner system, so as to reduce the pressure of the partner system to a certain extent

4:In order to mitigate the "fragmentation", it is often possible to put small chunks of memory in2are organized in multiples, similar to the partner system

2.slabthe concrete idea of the algorithm

The idea of

slab memory is divided into unequal sizes slabs chunks slabs slab is divided into equal size chunks slab chunk of Background:white " size is unequal ). chunk start from a minimum number in turn, Grows by a factor until the maximum possible value is reached.

  If the minimum value is Span style= "Background:white" >400b 1MB , Factor is 1.20 slab chunk slab1-400b slab2-480b slab3-576b

when assigning an object, select the appropriate object based on its size Slab , in the corresponding Slab assigns an object if the corresponding Slab when there is no free space, then ask the system for a Slab , in the allocation.

when the object is disposed, the object needs to be directly Slab you can set the object in the idle state.

This idea can be used flexibly, the main purpose is to classify it according to the frequency of the memory area, create a set of specific size of the dedicated buffer to deal with, in order to avoid the generation of fragments. For less -used memory areas, you can create a common set of buffers to handle (such as large memory allocations in our code), even if the processing pattern is fragmented, and has little impact on the performance of the system as a whole.

Linux Kernel pair Slab Application of:

Memcache the Slab Application of:

3.UseSlabthe pros and cons

Advantages: Reduce memory fragmentation and improve access efficiency.

disadvantage: Universal slab implementation, will waste about the space (such as our memcache in the slab);

4.slabwith theMalloc/freeCompare

Application Layer the implementation of Slab is actually implemented entirely on malloc . Simply saying that using slab to allocate memory is to reduce the number of malloc calls, and the benefit of reducing the number of calls to malloc is the value of slab or the key point of its performance. Therefore, understanding the mechanism of malloc is necessary to understand the existence of slab.

fewer calls to malloc: Reduce memory fragmentation, reduce page requests, reduce the number of cache breaches, reduce free, reduce page swapping (swap)

related performance points of consumption: memory merge, page adjustment, Cache Write

The specific details are interesting to study on their own.

5.of ourSlab

Before we look at the code, we'll look at a set of data:

uses slab(circled data) and Malloc/free(the data in the box) to allocate memory time consumption in the same size as the same number of times. The data is too small, and the memory is insufficient to fully reflect the actual situation, but it can prove that the slab is improving in performance.

6.optimizations and improvements that can be made

More to our needs, we can add some optimization strategies to make our memory utilization the highest. However, different optimization strategies require additional data that may require additional threads for statistics and analysis, while memory utilization may rise, but system efficiency may decrease. So this is a double-edged sword that can be more my resource and need to choose a balance point.

1. Redistribution

2. Peak Release

3. Develop single threaded multithreading allow mode

Slab Memory Management Sharing

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.