[talk] Does swap still matter?

John Baldwin jhb at FreeBSD.org
Thu Apr 7 13:32:18 EDT 2022


On 3/16/22 9:25 AM, Miles Nordin wrote:
>> When RAM never seemed adequate for a system in the past, the utility of
>> swap was obvious.
> 
> idea #1:
> 
> The measure of used RAM isn't obvious.  RAM allocated by sbrk or mmap is
> not used until it's written to for the first time.  (or maybe read, I am
> not sure)
> 
> what's more interesting wrt swap is, on some mallocs, like at least older
> GNU malloc, freed RAM is never returned to the OS.  tcmalloc does return
> RAM to the OS, but on some kind of lazy background thread or something.  so
> if a C program uses a whole bunch of RAM at startup then frees it, the physical
> RAM can only actually be freed by writing it to swap (then never reading it),
> or by the process exiting.

[ I know this is an old thread. ]

This is not quite correct in modern allocators.  While allocators generally
do not return virtual address space back to the OS via munmap(), they do
use madvise() (e.g. MADV_FREE on FreeBSD, OS X and Linux have similar
extensions) to let the OS reclaim the backing RAM for free'd memory.

FWIW, I still configure swap on systems I install, but really it's just to
allocate space to hold kernel crashdumps, not because I really need/want
swap.

Swap can save you a bit by not letting the OOM start killing processes
right away but give you time to login and be more selective before the
kernels starts killing processes itself.  For example, if you have a nagios
alert or the like for when swapping begins then having some swap available
might give you time to more gracefully deal with the problem.

-- 
John Baldwin




More information about the talk mailing list