Slab 分配器

 1、什么是Slab 分配器:


        Slab  firstly introduced in kernel 2.2, it’s now one of three memory allocator implementations together with SLOB and SLUB. The three allocators provides a kind of front-end to the zoned buddy allocator for those sections of the kernel that require more flexible memory allocation than the standard 4KB page size

        With slab allocation, a cache for a certain type or size of data object has a number of pre-allocated “slabs” of memory; within each slab there are memory chunks of fixed size suitable for the objects.The slab allocator keeps track of these chunks, so that when it receives a request to allocate memory for a data object of a certain type, usually it can satisfy the request with a free slot (chunk) from an existing slab. When the allocator is asked to free the object’s memory, it just adds the slot to the containing slab’s list of free (unused) slots. The next call to create an object of the same type (or allocate memory of the same size) will return that memory slot (or some other free slot) and remove it from the list of free slots. This process eliminates the need to search for suitable memory space and greatly alleviates memory fragmentation. In this context, a slab is one or more contiguous pages in the memory containing pre-allocated memory chunks.



 2、如何理解这个slab 机制:    

        我对Slab 分配器的理解是,slab 层应该是软件层面的一种内存管理机制,它把不同的对象划分为不同的高速缓存组(空闲链表),每一个高速缓存组针对一种类型的对象,例如一个只存task_struct的高速缓存组。
        Slab allocation 相当于给内存做了一层缓存,释放对象也不是真的释放掉,而是给个标记,如果下次同类型的对象要申请空间,直接在slab里面找标记为空闲的某块区域,直接分配。这样可以高效的应对小空间对象频繁的申请和释放。