问题描述
kmem_cache_alloc
和kmalloc()
在内核内存分配上有什么区别?何时使用哪一个?
What is difference between kmem_cache_alloc
and kmalloc()
in kernel memory allocation? which one is used when?
推荐答案
Kmalloc -从物理内存分配连续区域.但是请记住,分配和释放内存是很多工作.
Kmalloc - allocates contiguous region from the physical memory. But keep in mind, allocating and free'ing memory is a lot of work.
Kmem_cache_alloc -在这里,您的进程保留了一些预分配大小的对象的一些副本.假设您已经知道自己将经常需要该结构,所以您不必保留它的多个副本,而无需在需要时从主内存(kmalloc)对其进行分配.如果需要,它将返回已分配的块的地址(节省大量时间).同样,当您释放它时,您不会将其退还,实际上它并未被释放,它会返回到分配的池中,这样,如果某个进程再次要求它,则可以返回已经分配的地址结构.
Kmem_cache_alloc - Here, your process keeps some copies of the some pre-defined size objects pre-allocated. Say you have struct that you know you will be requiring very frequently, so instead of allocating it from the main memory (kmalloc) when you need it, you already keep multiple copies of it allocated & when you want it, it returns the address of the block already allocated (saves a lot of time). Similarly, when you free it, you don't give it back, it actually isn't free'd, it goes back to the allocated pool so that if some process again asks for it, you can return this address of the already allocated struct.
这篇关于kmalloc和kmem_cache_alloc之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!