Slab allocation
From Wikipedia, the free encyclopedia
This article may need to be rewritten entirely to comply with Wikipedia's quality standards. You can help. The discussion page may contain suggestions. |
Slab allocation is a memory management mechanism intended for more efficient memory allocation and to eliminate memory fragmentation to a large extent. The basis for this algorithm is retaining an allocated memory that used to contain a data object of certain type and reusing that memory for the next allocations for another object of the same type. This technique was first introduced in SunOS by Jeff Bonwick and now is widely used by many Unix operating systems including Linux.
Contents |
[edit] Basis
The fundamental idea behind slab allocation technique is based on the observation that there are some kernel data objects that 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.
With slab allocation, using certain system calls by the programmer, memory chunks suitable to fit data objects of certain type or size are preallocated. The slab allocator keeps track of these chunks known as caches so that when a request to allocate memory for a data object of certain size is received it can instantly satisfy the request with an already allocated slot. Destruction of the object however, does not free up the memory, but only opens a slot which is put in the list of free slots by the slab allocator. The next call to allocate memory of the same size, will return the (now unused) memory slot. This process eliminates the need to search for the suitable memory space and alleviates memory fragmentation to a large extent. In this context a slab is one or more contiguous pages in the memory containing pre-allocated memory chunks.
[edit] Implementation
Understanding the slab allocation algorithm requires defining and explaining some terms:
- Cache: cache represents a small amount of very fast memory. Here we use cache as storage for objects such as semaphores, process descriptors, file objects etc. Every cache represents storage for only one type of object.
- Slab: slab represents a contiguous piece of memory, usually made of several physically contiguous pages. A cache consists of one or more slabs.
When a program sets up a cache, it allocates a number of objects to that cache. This number depends on the size of the associated slabs.
Slabs may exist in one of the following states :
- empty - all objects on a slab marked as free
- partial - slab consists of both used and free objects
- full - all objects on a slab marked as used
Initially, the system marks each slab as "empty". When the process calls for a new kernel object, the system tries to find a free location for that object on a partial slab in a cache for that type of object. If no such location exists, the system allocates a new slab from contiguous physical pages and assigns it to a cache. The new object gets allocated from this slab, and its location becomes marked as "partial".
The slab allocation algorithm has as its principal benefit that memory gets allocated in exactly the same size as requested, thus no internal memory fragmentation exists. The allocation takes place quickly, because the system builds the objects in advance and readily allocates them from a slab.
[edit] Slabs
A slab is the amount that a cache can grow or shrink by. It represents one memory allocation to the cache from the machine, and whose size is customarily a multiple of the page size. A slab must contain a list of free buffers (or bufctls), as well as a list of the bufctls that have been allocated (in the case of a large slab size).
[edit] Large slabs
These are for caches that store objects that are not less than 1/8 of the page size for a given machine. The reason for the large slabs having a different layout from the small slabs is that it allows large slabs to pack better into page-size units, which helps with fragmentation. The slab contains a list of bufctls, which are simply controllers for each buffer that can be allocated (a buffer is the memory that the user of a slab allocator would use).
[edit] Small slabs
The small slabs contain objects that are less than 1/8 of the page size for a given machine. These small slabs need to be optimized further from the logical layout, by avoiding using bufctls (which would be just as large as the data itself and cause memory usage to be much greater). A small slab is exactly one page, and has a defined structure that allows bufctls to be avoided. The last part of the page contains the 'slab header' which is the information needed to retain the slab. Starting at the first address of that page, there are as many buffers as can be allocated without running into the slab header at the end of the page.
Instead of using bufctls, we use the buffers themselves to retain the free list links. This allows the small slab's bufctl to be bypassed.
[edit] Systems using slab allocation
- AmigaOS (introduced in 4.0)
- DragonFly BSD (introduced in release 1.0)
- FreeBSD (introduced in 5.0)
- Linux (introduced in kernel 2.2, many popular distributions now choose the SLUB allocation method over SLAB, but it is still available as an option)
- NetBSD (introduced in 4.0)
- Solaris (introduced in 2.4)
- Jari OS
[edit] See also
- Slab (NCR) - a similar but distinct meaning for NCR computers
- SLUB (computer science)
[edit] Sources
- Abraham Silberschatz et al: Operating system concepts. Wiley: 2004. ISBN 0-471-69466-5
- Jeff Bonwick, The Slab Allocator: An Object-Caching Kernel Memory Allocator (1994)
- FreeBSD uma(9) manual page
[edit] External links
- Anatomy of the Linux slab allocator a developerWorks article by M. Tim Jones
- The SLUB allocator comment about management of slabs in Linux by two different allocators: SLUB allocator and SLAB allocator