Slab Cache

http://www.ibm.com/developerworks/linux/library/l-linux-slab-allocator/?ca=dgr-lnxw16LinuxSlabAllo

 

 

这个文章不错。

 

经过这两天的学习,了解到,在kernel中分配内存 至少有两种方式:

1. kmalloc

2. kmem_cache

 

其实都是用了kmem_cache。 只是用kmem_cache_alloc的时候,用了一种比较特殊的,有名字定义的kmem_cache。

 

而且这两种申请方式,都可以从/proc/slabinfo文件中看到。

 

 

下面是我写的一个解释。。。

I did some reading and experiment on memory allocation in kernel space. Hope this could be useful for you.
 
As in user space, we need allocate memory dynamically in kernel space, while it use different mechanism. In order to increase the efficiency and alleviate the fragmentation, kernel always allocate fixed size memory from different memory pools, called slab.
 
There are two common memory allocation method in kernel space.
Method 1. kmalloc, kfree.
Method 2. kmem_cache_create, kmem_cache_alloc, kmem_cache_free, kmem_cache_destroy.
 
Apparently, these are two forms, but the idea behind them is the same. I try to draw an analogy to make it clear.
 
If we think kernel as a factory, memory as boxes we want from the factory.
This factory has some pre-produced boxes with several fixed size boxes. If this can meet your needs, ok, just ask for this(use method 1).
While some time this may not meet your need, such as size is not accurate or you need a huge amount of boxes, you could design your own box, and ask the factory to produce it(use method 2).
 
One fun thing is the factory(kernel), also provides you a way to track the "order". In /proc/slabinfo, you can see your boxes produced(active) and boxes in warehouse(total numbers).
For method 1, you see these in the bottom of the file, start with "size-", with smallest size just fit your requirement.
For method 2, you see these lines start with the name you defined in kmem_cache_create.
 
Attachment is the kernel module I used for experiment, you can make it and run on our board. To see what happens in /proc/slabinfo.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值