How much RAM needed for preventing use swap?

FreeBSD divides memory into 5 lists:

  • wired (locked in memory, unpageable)
  • active (recently used)
  • inactive (not recently used)
  • cache
  • free

Some of those have minimum free and target free levels, e.g.

Pool     Minimum Target
Free       0.7%    3%
Cache      3%      6%
Inactive   0%      4.5%

(Source: The Design and Implementation of the FreeBSD Operating System', rather dated: 2004 covering FreeBSD 5.2 -- these numbers have changed since then, but the general approach has not I believe.)

The pagedaemon is tasked with maintaining the levels, and paging out as need.

From a FreeBSD 9.0 VM I have here, these are some of the relevant numbers:

hw.availpages: 3138511
vm.stats.vm.v_inactive_target: 121342     # 3.9%
vm.stats.vm.v_free_target: 80895          # 2.6%
vm.stats.vm.v_free_reserved: 4011         # 0.1%
vm.stats.vm.v_free_min: 19221             # 0.6%
vm.stats.vm.v_cache_min: 80895            # 2.6%
vm.stats.vm.v_cache_max: 161790           # 5.1%

(I'm not certain how they are calculated, it depends on physical memory and presumably kern.maxusers).

FreeBSD will proactively pageout to increase available cache so just because swap is used doesn't mean your system is under pressure. It's far more useful to inspect (and graph!) the rates of paging and other indicators of load such disk I/O and CPU ticks, systat -io is a start. Running locate/updatedb is a good way to shift memory usage around overnight.

Using swap even when there is free memory is normal behaviour, it's effectively preparing for possible future memory demands while idle, so swap usage alone is not a reliable way to determine RAM requirements. FreeBSD is quite good at telling you when you need to tune something (grep "see tuning" in the messages file).

Other useful reading:

  • http://www.freebsd.org/doc/en/articles/vm-design/index.html
  • https://wiki.freebsd.org/SystemTuning