Slab allocation is a memory management mechanism intended for the efficient memory allocation of objects. Compared to earlier mechanisms, it reduces fragmentation caused by allocations and deallocations. The technique is used to retain allocated memory that contains a data object of a certain type for reuse upon subsequent allocations of objects of the same type. It is analogous to an object pool, but only applies to memory, not other resources.
The primary motivation for slab allocation is that the cost (in CPU time) of initialization and destruction of kernel data objects can outweigh the cost of allocating memory for them. As the kernel creates and deletes objects often, overhead costs of initialization can result in significant performance drops. Object caching leads to less frequent invocation of functions that initialize object state: when a slab-allocated object is released after use, the slab allocation system typically keeps it cached (rather than doing the work of destroying it) ready for re-use next time an object of that type is needed (thus avoiding the work of constructing and initialising a new object).
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.
Understanding the slab allocation algorithm requires defining and explaining some terms:
- Cache: cache represents a small amount of very fast memory. A cache is a storage for a specific type of object, such as semaphores, process descriptors, file objects, etc.
- Slab: slab represents a contiguous piece of memory, usually made of several physically contiguous pages. The slab is the actual container of data associated with objects of the specific kind of the containing cache.
When a program sets up a cache, it allocates a number of objects to the slabs associated with 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 allocation takes place quickly, because the system builds the objects in advance and readily allocates them from a slab.
A slab is the amount by which a cache can grow or shrink. 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).
These are for caches that store objects that are at least 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).
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.
Systems using slab allocation
- AmigaOS (introduced in AmigaOS 4)
- DragonFly BSD (introduced in release 1.0)
- FreeBSD (introduced in 5.0)
- GNU Mach 
- Haiku (introduced in alpha 2)
- Horizon (Nintendo Switch microkernel)
- HP-UX (introduced in 11i)
- Linux (introduced in kernel 2.2, some distributions use SLUB allocation method over SLAB, but SLAB has better NUMA performance) — In Linux, slab allocation 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
- NetBSD (introduced in 4.0)
- Solaris (introduced in 2.4)
- The Perl 5 compiler uses a slab allocator for internal memory management
- Memcached uses slab allocation for memory management
- Jeff Bonwick,The Slab Allocator: An Object-Caching Kernel Memory Allocator (1994)
- FreeBSD Kernel Developer's Manual Archived 21 July 2011 at the Wayback Machine
- M. Tim Jones, Anatomy of the Linux slab allocator Archived 2 October 2013 at the Wayback Machine
- Abraham Silberschatz et al.: Operating system concepts. Wiley: 2004. ISBN 0-471-69466-5
- "Gnu Mach Allocator Documentation".
- "Console Security - Switch (34c3)". media.ccc.de. Retrieved 28 December 2017.
- Chris Cooper and Chris Moore, HP-UX 11i Internals, Upper Saddle River, New Jersey: Prentice Hall PTR, 2004, ISBN 0-13-032861-8, p. 334.
- Hutchings, Ben (29 March 2012). "Re: CONFIG_SLAB=y in 3.2 kernel".
- "Perl5-Porters Weekly: 2012 June 17". Retrieved 18 November 2012.
- Bonwick, Jeff. "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources". USENIX 2001. Retrieved 18 November 2012.
- FreeBSD uma(9) manual page
- The SLUB allocator comment about management of slabs in Linux by two different allocators: SLUB allocator and SLAB allocator
- Memory Compaction v7 (a Linux patch set from Mel Gorman dealing with SLAB fragmentation and compaction issues, 2 April 2010)
- Detecting kernel memory leaks Jonathan Corbet, Linux Weekly News, 2006; includes user comments on garbage collection
- Linux performance: is Linux becoming just too slow and bloated? On SLAB and SLUB. Free software magazine 2010.
-  — includes the
slabtoputility to display kernel slab cache information in real time