Difference between revisions of "Swap/en"

Updating to match new version of source page
(Updating to match new version of source page)
(Updating to match new version of source page)
Line 19: Line 19:
=Do I Need Swap=
=Do I Need Swap=


That is a question that cannot be answerd without having a look at your configuration and even with plenty of available memory, it is often used as a safety net or even sometimes due to specific application requirements so have a look at the following non-exhaustive list:
That is a question that cannot be answered without having a look at your configuration and even with plenty of available memory, it is often used as a safety net or even sometimes due to specific application requirements so have a look at the following non-exhaustive list:
* If you use hibernation: ''yes, you need swap!''
* If you use hibernation: ''yes, you need swap!''
* If you have services that are not always active, but are still running all the time: ''yes, you need swap!''
* If you have services that are not always active, but are still running all the time: ''yes, you need swap!''
Line 29: Line 29:
The amount of swap you need is highly variable based on ''your'' specific applications and workload.  There is no universal formula on swap size without monitoring usage over a period of time.  A reasonable place to start would be:
The amount of swap you need is highly variable based on ''your'' specific applications and workload.  There is no universal formula on swap size without monitoring usage over a period of time.  A reasonable place to start would be:
* For less then 4GB of physical memory (RAM), it's highly recommended that the swap space should, as a base minimum, be equal to the amount of RAM. Also, it's recommended that the swap space is maximum twice the amount of RAM depending upon the amount of disk space available for the system because of diminishing returns.
* For less then 4GB of physical memory (RAM), it's highly recommended that the swap space should, as a base minimum, be equal to the amount of RAM. Also, it's recommended that the swap space is maximum twice the amount of RAM depending upon the amount of disk space available for the system because of diminishing returns.
* For more modern systems (>4GB), your swap space should be at a minimum be equal to your physical memory (RAM) size '''if you use hibernation''', otherwise you need a minimum of ROUNDUP(SQRT(RAM)) I.E. the square root of your RAM size rounded up to the next GB. The maximum, is again twice the amount of RAM, again because of diminishing returns.
* For more modern systems (>4GB), your swap space should be at a minimum be ROUNDUP(SQRT(RAM)) I.E. the square root of your RAM size rounded up to the next GB.  '''However, if you use hibernation''', you need a minimum of physical memory (RAM) size '''plus''' ROUNDUP(SQRT(RAM)). The maximum, is again twice the amount of RAM, again because of diminishing returns.
* The only downside to having more swap space than you will actually use, is the disk space you will be reserving for it cannot be used for application or system data.
* The only downside to having more swap space than you will actually use, is the disk space you will be reserving for it cannot be used for application or system data.


Line 59: Line 59:
         4TB            64GB              4160GB      8TB
         4TB            64GB              4160GB      8TB
         8TB            91GB              8283GB    16TB
         8TB            91GB              8283GB    16TB
{{note|The largest server one of the authors of this wiki has ever installed had, indeed, 8TB of RAM}}
{{note|The largest server one of the authors of this wiki article has ever installed had, indeed, 8TB of RAM and even that machine has the above swap settings, so why not your machine?}}
=Displaying Swap Information=
=Displaying Swap Information=


Line 68: Line 68:


The following script will:
The following script will:
* show whether zswap is active or not and if active, give zswap parameters if run with the {{$ic|sudo}} command
* show whether zswap is active or not and if active, give zswap parameters if run with the {{ic|sudo}} command
* display a list of all applications / services that take up swap and how much they take up in descending order
* display a list of all applications / services that take up swap and how much they take up in descending order


Line 118: Line 118:


First create and intialize the file to hold the swap.  For example, to create a 4GB swapfile, you could use the command:
First create and intialize the file to hold the swap.  For example, to create a 4GB swapfile, you could use the command:
  sudo fallocate -l 4G /swapfile
 
sudo mkswap /swapfile
  sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress




Set the appropriate permissions on the file.  It should be readable and writable only by {{ic|root}}.  This can be done with the command:
Set the appropriate permissions on the file.  It should be readable and writable only by {{ic|root}}.  This can be done with the command:
sudo chmod u=rw,go= /swapfile


sudo chmod 600 /swapfile
Next we need to format and enable the swapfile:
sudo mkswap /swapfile


Next we need to enable the swapfile with the {{ic|swapon}} command.  Following our example above this could be done with:
  sudo swapon /swapfile
  sudo swapon /swapfile




In order to ensure that the swap is enabled at boot we can add an entry to {{ic|/etc/fstab}}.  You can add the line to fstab manually or using the command:
In order to ensure that the swap is enabled at boot we can add an entry to {{ic|/etc/fstab}}.  You can add the line to fstab manually or using the command:
  sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"
  sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"


==Swapfiles on BTRFS==
==Swapfiles on BTRFS==
Line 212: Line 218:
zswap is compressed swap kept in RAM. zswap keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk.  In many workloads, this will result in increased swap performance and less wear and tear on SSDs.
zswap is compressed swap kept in RAM. zswap keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk.  In many workloads, this will result in increased swap performance and less wear and tear on SSDs.


To enable zswap with systemd-swap, simply alter the instructions above and set Linux Kernel parameter {{ic|zswap_enabled=1}}
To enable zswap with systemd-swap, simply alter the instructions above and set Linux Kernel parameter zswap_enabled=1
=Tuning & Performance Considerations=
=Tuning & Performance Considerations=


Line 223: Line 229:
{{ic|swappiness}} controls how likely a page is to be transferred to swap.  This value represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory where:
{{ic|swappiness}} controls how likely a page is to be transferred to swap.  This value represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory where:
* 0 disables swap
* 0 disables swap
* 60 is the default value which is ideal for a server running a lot of services
* 60 is the default value which is ideal for a server that is memory-starved and running a lot of services
* 100 is very aggressive swapping.
* 100 is very aggressive swapping.


For ''most'' computers the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent.
For ''most'' desktop computers the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent.


{{ic|vfs_cache_pressure}} is a percentage value that controls the tendency of the kernel to ''reclaim'' the memory which is used for caching of directory and inode objects.  The default value is 100.  Increasing this value will increase the rate in which these objects are removed from the RAM cache.  Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time.  Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance.  Experimentation is needed to find the appropriate balance and the default value is reasonable.  In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure.
{{ic|vfs_cache_pressure}} used to be a percentage value that controls the tendency of the kernel to ''reclaim'' the memory which is used for caching of directory and inode objects with a default value of 100 on Kernels <5.4. On Kernels >=5.4 can be increased beyond 100.  Increasing this value will increase the rate in which these objects are removed from the RAM cache.  Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time.  Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance.  Experimentation is needed to find the appropriate balance and the default value is reasonable.  In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure.


To set these values you can use the command {{ic|sysctl}}.  For example, to set the swappiness value to 10 you could use:
To set these values you can use the command {{ic|sysctl}}.  For example, to set the swappiness value to 10 you could use:
Line 247: Line 253:




[[Category:Contents Page]]
[[Category:Contents Page{{#translation:}}]]
8,138

edits