How much RAM needed for preventing use swap?

6,574

Solution 1

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:

Solution 2

if you have spare memory increasing memory not working you should change the swappiness parameter. it tells the kernel how often use non physical memory (swap).

check swappiness value:

cat /proc/sys/vm/swappiness

change the swappiness value temporary (lost on reboot) to value 10 (good value that decreases swap usage)

sudo sysctl vm.swappiness=10

if you want to change it permanent use config file:

sudo vim /etc/sysctl.conf

search for vm.swappiness and change it to desired value.

vm.swappiness=10

save and reboot.


desciption:

what is swappines?

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

swappiness can have a value of between 0 and 100

swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible

swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

Share:
6,574

Related videos on Youtube

moronizzz
Author by

moronizzz

Updated on September 18, 2022

Comments

  • moronizzz
    moronizzz almost 2 years

    At my system always available some "Free" and "Inactive" memory, but sometimes system move couple megabytes to swap. Per night it maybe around 100MB, but for a few days in swap could be more than 4000MB.

    How can I determine required memory for my system?

    • Admin
      Admin almost 10 years
      Looks like you've answered your own question in the question itself. You've got a good handle on what your setup needs. If you had another 4GB you'd probably hardly ever hit the swap drive. It is possible that the service using this space prefers the swap to RAM and that getting more RAM will not affect your swap usage but that is rare.
    • Admin
      Admin almost 10 years
      Seems a bit strange that your swap should increase regularly. Is this a server with a regular workload or a workstation/test env.?
    • Admin
      Admin almost 10 years
      @krowe I have already added RAM to this server, but this doesn't solve the problem.
    • Admin
      Admin almost 10 years
      @Mat This is server with many apache prefork instances with mod_php. Many of these use APC with allocated memory 64-256MB. Apache handlers have from ~150MB to ~450MB "virtual size", wich changes depending on query to it and allocated memory for APC.
    • Admin
      Admin almost 10 years
      @moronizzz Once again you have the answer already. You've increased RAM and saw no improvement. That means you have more than enough RAM. The fact that the swap is being used on occasion is perfectly normal and optimal. You could move the swap to RAM like this: # mkswap /dev/ram0 # swapoff -a # swapon /dev/ram0 but that is a very bad idea because your system will most likely hang if you ever exceeded you physical memory. Another (much better) option would be to just put your swap on an SSD.
  • Mat
    Mat almost 10 years
    Please always cite the sources you quote - at least an URL pointing to that documentation if it's online, or the title and author of the work otherwise. AFAIK, what you say is for Linux only. Question is tagged FreeBSD. Does it apply there?
  • amir jj
    amir jj almost 10 years
  • Mat
    Mat almost 10 years
    You should edit the link into your question. FreeBSD isn't a Linux at all.
  • amir jj
    amir jj almost 10 years
    posted another answer for freeBSD.
  • Mat
    Mat almost 10 years
    The question isn't about enabling/disabling swap, I don't really see how this answers it.
  • amir jj
    amir jj almost 10 years
    @Mat. so i don't really get this question. i saw a server with 32 RAM and just started without any heavy services on it but swap wasn't empty. so i answer this question like this if you don't want use swap, disable it because kernel will use it. the kernel do not use swap arbitrary. swap will not be used if the memory is truly idle. i think kernel will move unused pages to swap regardless of memory size. see freebsd.org/doc/en_US.ISO8859-1/books/faq/misc.html#idp68642‌​992
  • moronizzz
    moronizzz almost 10 years
    @amirjj I just want to determine how much memory needed for server because sometimes processes killed with error "out of swap space".
  • plas
    plas about 5 years
    This does not apply to FreeBSD at all and, therefore, is not an answer to the question.
  • plas
    plas about 5 years
    This does not apply to FreeBSD at all and, therefore, is not an answer to the question.