A plain language description of the memory stats seen in places like top(1) and sysctl output, designed specifically for a non-developer audience.
Overview
FreeBSD's VM operates on memory in units of pages, which have a size of 4KB on most platforms.
FreeBSD uses a set of 3 queues to manage pageable memory. The size of each queue (Active, Inactive, and Laundry) is visible in top(1).
Pageable memory consists of anonymous memory and file data. Anonymous memory has no dedicated backing storage, and will be written to the swap device if the memory needs to be reused for some other purpose. If the swapped-out memory is referenced again, some free memory will be allocated, and the saved contents of the swapped-out memory will be read back in. Examples of anonymous memory include but are not limited to:
- memory returned by malloc() to a userland application
- the contents of a MD device of type "swap" (see mdconfig(8))
- tmpfs filesystems
- SysV or POSIX shared memory segments
File data is simply the cached contents of files, and file metadata. In general, filesystems will maintain their own fixed-size cache of file data and metadata. With UFS, msdosfs, NFS and others, this is called the "buffer cache"; with ZFS this is the ARC (adaptive replacement cache). When memory is evicted from the buffer cache to make room for new data, it is placed in the inactive queue. Memory evicted from the ARC is simply freed immediately and never enters the page queues.
Pages belonging to page queues are in one of two states: clean or dirty. Dirty pages must have their contents saved before they can be reused for some other purpose, at which point they become clean. Dirty anonymous pages are cleaned by writing their contents to the swap device, and dirty file pages are cleaned by writing their contents to the filesystem's backing storage. Once a page is clean, it can be easily freed and reused.
Memory Classes
Active
- Contains pages "actively" (recently) referenced by userland
- Contains a mix of clean and dirty pages
- Pages are regularly scanned by the page daemon (each page is visited once every vm.pageout_update_period seconds)
- Scans check to see if the page has been referenced since the last scan
- If enough scans complete without seeing a reference, the page is moved to the inactive queue
- Implements pseudo-LRU
Inactive
- Contains pages aged out of the active queue
- Contains pages evicted from the buffer cache
- Contains a mix of clean and dirty pages
- Pages are scanned by the page daemon (starting from the head of the queue) when there is a memory shortage:
- Pages which have been referenced are moved back to the active queue or the tail of the inactive queue
- Pages which are dirty are moved to the tail of the laundry queue
- Unreferenced, clean pages may be freed and reused immediately
- Implements second-chance LRU
Laundry
- Queue for managing dirty inactive pages, which must be cleaned ("laundered") before they can be reused
- Managed by a separate thread, the laundry thread, instead of the page daemon
- Laundry thread launders a small number of pages to balance the inactive and laundry queues
- Frequency of laundering depends on:
- How many clean pages the page daemon is freeing; more frees contributes to a higher frequency of laundering
- The size of the laundry queue relative to the inactive queue; if the laundry queue is growing, we will launder more frequently
- Pages are scanned by the laundry thread (starting from the head of the queue):
- Pages which have been referenced are moved back to the active queue or the tail of the laundry queue
- Dirty pages are laundered and then moved close to the head of the inactive queue
Free
- Memory available for use by the rest of the system.
Wired
- Non-pageable memory: cannot be freed until explicitly released by the owner
- Userland memory can be wired by mlock(2) (subject to system and per-user limits)
- Kernel memory allocators return wired memory
- Contents of the ARC and the buffer cache are wired
- Some memory is permanently wired and is never freed (e.g., the kernel file itself)