slab allocation

{{Short description|Memory management mechanism}}

{{distinguish|Slab (unit)}}

{{Use dmy dates|date=July 2022}}

Slab allocation is a memory management mechanism intended for the efficient memory allocation of objects. In comparison with earlier mechanisms, it reduces fragmentation caused by allocations and deallocations.

This technique is used for retaining allocated memory containing 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.

Slab allocation was first introduced in the Solaris 2.4 kernel by Jeff Bonwick. Bonwick claims the name "Slab" comes from a Kellogg's cereal commercial catchphrase rhyme, "grab a slab".{{cite web |last=Bonwick |first=Jeff |date=June 14, 2005 |title=The story behind the slab allocator |url=https://blogs.oracle.com/bonwick/entry/now_it_can_be_told |website= |location= |publisher=Oracle |url-status= |archive-url=https://web.archive.org/web/20160304134711/https://blogs.oracle.com/bonwick/entry/now_it_can_be_told |archive-date=March 4, 2016 |access-date=March 27, 2025}}

Slab allocation is now widely used by many Unix and Unix-like operating systems including FreeBSD[https://www.freebsd.org/cgi/man.cgi?query=zone&apropos=0&sektion=9&manpath=FreeBSD+12.1-RELEASE&arch=default&format=html FreeBSD Kernel Developer's Manual] and Linux,M. Tim Jones, [http://www.ibm.com/developerworks/linux/library/l-linux-slab-allocator/ Anatomy of the Linux slab allocator] {{webarchive |url=https://web.archive.org/web/20131002111628/http://www.ibm.com/developerworks/linux/library/l-linux-slab-allocator/ |date=2 October 2013}} both in the SLAB allocator and its replacement, SLUB.Vlastimil Babka, [https://lore.kernel.org/linux-mm/ZXEx1%2Fp9ejRmkVTS@localhost.localdomain/T/#m1a5899625baa61ad31a0e99eea6fc02258513ac1 remove the SLAB allocator]

Basis

Slab allocation significantly reduces the frequency of computationally costly initialization and destruction of kernel data-objects, which can outweigh the cost of allocating memory for them.Jeff Bonwick, [http://www.usenix.org/publications/library/proceedings/bos94/full_papers/bonwick.ps The Slab Allocator: An Object-Caching Kernel Memory Allocator (1994)] When 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 which 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.Abraham Silberschatz et al.: Operating system concepts. Wiley: 2004. {{ISBN|0-471-69466-5}} 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.

Implementation

The slab allocation algorithm defines the following terms:

  1. 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.
  2. Slab: slab represents a contiguous piece of memory, usually made of several virtually 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:

  1. empty – all objects on a slab marked as free
  2. partial – slab consists of both used and free objects
  3. 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 virtual 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.

Implementation techniques

=Free lists=

A slab represents one memory allocation to the cache from the machine, and whose size is customarily a multiple of the page size. The slab will be divided into a number of entries, which will then be requested by the cache as the client code requests memory for new objects. It is necessary then to keep track of which parts of the slab are free to use and which ones were already occupied. This is generally done using "free lists": lists of free entries in the slab ready to store new objects.

The free list may be a separate data structure, such as an array of indices indicating which entries of the slab are free, or it may be embedded within the slab. The Linux SLUB allocator keeps the free list as a linked list of pointers, each of which is stored directly in the free memory area of the slab they represent.{{cite web |last1=Lameter |first1=Christoph |title=Slab allocators in the Linux Kernel: SLAB, SLOB, SLUB |url=https://events.static.linuxfound.org/sites/events/files/slides/slaballocators.pdf |website=LinuxCon/Düsseldorf 2014 (Revision Oct 3, 2014)}}

=Slab sizes=

Operating systems may use different slab sizes and internal layouts depending on the size of the objects to be stored. 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. For example, objects that are at least 1/8 of the page size for a given machine may benefit from a "large slab" size, with explicit free lists, while smaller objects may use a "small slab" setup, embed the free list tracking. Bonwick's original presentation of the slab allocator already made the distinction of layouts for large and small slabs.

Systems using slab allocation

  • AmigaOS (introduced in AmigaOS 4)
  • DragonFly BSD (introduced in release 1.0)
  • FreeBSD (introduced in 5.0)
  • GNU Mach{{cite web|title=Gnu Mach Allocator Documentation|url=https://www.gnu.org/software/hurd/microkernel/mach/gnumach/memory_management.html}}
  • Haiku (introduced in alpha 2)
  • Horizon (Nintendo Switch microkernel){{cite web|title=Console Security – Switch (34c3)|url=https://media.ccc.de/v/34c3-8941-console_security_-_switch|website=media.ccc.de|accessdate=28 December 2017}}
  • HP-UX (introduced in 11i)Chris Cooper and Chris Moore, HP-UX 11i Internals, Upper Saddle River, New Jersey: Prentice Hall PTR, 2004, {{ISBN|0-13-032861-8}}, [https://books.google.com/books?id=SsH-qdTx9pUC&dq=hp-ux+arena+allocator&pg=PA334 p. 334].
  • Linux (introduced in 2.1.23)
  • NetBSD (introduced in 4.0{{Citation needed|date=May 2025|reason=There is no mention of a slab allocator in the NetBSD 4.0 release notes, and Matthew Green claims in personal communication that NetBSD's slab allocator (called a 'pool allocator' by NetBSD) was added in 1.4.}})
  • Solaris (introduced in 2.4)
  • The Perl 5 compiler uses a slab allocator for internal memory management{{cite web|title=Perl5-Porters Weekly: 2012 June 17|url=http://byte-me.org/perl5-porters-weekly-2012-june-17/|accessdate=18 November 2012}}{{cite web|last=Bonwick|first=Jeff|title=Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources|url=http://static.usenix.org/event/usenix01/full_papers/bonwick/bonwick_html/|publisher=USENIX 2001|accessdate=18 November 2012}}
  • Memcached uses slab allocation for memory management
  • illumos

See also

Notes

{{Reflist}}