Slab Allocator简介

vmlinz posted @ Wed, 04 May 2011 17:05:29 +0800 in Linux with tags linux MM , 6660 readers

Slab Allocator(对象缓存分配)

The fundamental idea behind slab allocation technique is based on the observation that some kernel data objects are frequently created and destroyed after they are not needed anymore. This implies that for each allocation of memory for these data objects, some time is spent to find the best fit for that data object. Moreover, deallocation of the memory after destruction of the object contributes to fragmentation of the memory, which burdens the kernel some more to rearrange the memory.Slab 算法的发现是基于内核中内存使用的一些特点:一些需要频繁使用的同样大小数据经常在使用后不久又再次被用到;找到合适大小的内存所消耗的时间远远大于释放内存所需要的时间。所以Slab算法的发明人认为内存对象在使用之后不是立即释放给系统而是将它们用链表之类的数据结构管理起来以备将来使用,频繁分配和释放的内存对象应该用缓存管理起来。Linux的slab分配器就是基于这样的想法实现的,这个算法在空间和时间上都有很高的效率。

Slab implementation




  • slabs_full,完全分配好的slab对象链表
  • slabs_partial,部分分配好的slab对象链表
  • slabs_empty,空对象链表,slab对象都没有分配好




  • 通过缓存类似对象数据,内核中频繁的小数据对象的分配不会再消耗过多的时间,同时减少了系统的内存碎片
  • slab分配支持常用对象数据的初始化,减少了同类对象数据重复的初始化过程
  • slab分配支持硬件缓存对齐和着色,这样不同缓存下的对象数据可以使用同样的硬件缓存行,可以提高系统的性能


  • struct kmem_cache *;缓存指针,用于分配,释放缓存中的数据对象
  • struct kmem_cache *kmem_cache_create( const char *name, size_t size, size_t align, unsigned long flags, void (*ctor)(void*, struct kmem_cache *, unsigned long), void (*dtor)(void*, struct kmem_cache *, unsigned long));创建缓存,ctordtor两个回调函数是提供给用户初始化和释放对象数据用的
  • void kmem_cache_destroy( struct kmem_cache *cachep );销毁缓存
  • void* kmem_cache_alloc( struct kmem_cache *cachep, gfp_t flags );从缓存中分配对象数据,其中的flagskmalloc函数使用的标记一样,用于控制缓存内部从buddy system中分配内存页面的行为
  • void kmem_cache_free( struct kmem_cache *cachep, void *objp );回收数据到缓存中的slab对象

  • void *kmalloc( size_t size, int flags );kmalloc和前面的从缓存中分配对象数据一样,只不过它不需要提供一个特定的缓存,它通过遍历系统中可用的缓存来分配对象数据。这样我们终于知道kmalloc的实现了,它是slab分配器的一个接口

  • void kfree( const void *objp );

further reading

buy custom term pape said:
Mon, 25 Mar 2019 15:23:34 +0800

The central impression behind slab allocation method depends upon the observation that some kernel data objects are often made and wrecked after they are not required anymore. We all can now buy custom term paper from them. This infers that for every allocation of memory for these data objects, sometime is invested to find the finest match for that data article.

Essay Help UK said:
Fri, 04 Oct 2019 15:34:01 +0800

Amazing at the same time, I will make - was staggering! Thankful to such a degree.

AAA said:
Sun, 22 May 2022 16:14:34 +0800

some sleeping bags are waterproof and weatherproof too, they are nice for camping outside the house** How to Be a Credit Card Processor

AAA said:
Sat, 28 May 2022 06:03:37 +0800

I must point out my appreciation for your generosity supporting women who absolutely need help with this question. Your special commitment to passing the solution throughout was remarkably functional and has continually helped associates just like me to attain their ambitions. Your amazing warm and helpful suggestions signifies this much to me and especially to my colleagues. Best wishes; from everyone of us. 먹튀검증

AAA said:
Sun, 05 Jun 2022 05:53:26 +0800

Good – I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs and related info ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or something, web site theme . a tones way for your customer to communicate. Excellent task. 腳架

AAA said:
Tue, 14 Jun 2022 19:14:24 +0800

Nice post , this is my first time i read this blog and i found it very interesting , nice job . macbook回收

qqhow said:
Fri, 24 Jun 2022 02:42:49 +0800 Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My blog is in the very same niche as yours and my visitors would genuinely benefit from a lot of the information you provide here. Please let me know if this okay with you. Thank you! diy pedicure
meidir said:
Fri, 29 Jul 2022 18:43:30 +0800

Good write-up normal visitor of one¡¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time. numero voyance gratuite

meidir said:
Mon, 03 Oct 2022 23:46:38 +0800

Oh my goodness! an amazing article dude. Thank you Even so My business is experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Can there be anyone obtaining identical rss problem? Anybody who knows kindly respond. Thnkx Macbook air

Login *

loading captcha image...
(type the code from the image)
or Ctrl+Enter