Linux/Swap Space: Difference between revisions
Brodriguez (talk | contribs) (Create page) |
Brodriguez (talk | contribs) m (Brodriguez moved page Linux Swap Space to Linux/Swap Space: Clean url with subpages) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Swap space is a section of hard drive space that is allocated specifically to be used as a memory (RAM) overflow.<br> | '''Swap space''' is a section of hard drive space that is allocated specifically to be used as a memory (RAM) overflow.<br> | ||
Swap memory isn't as fast as standard memory. So if having memory issues, the proper solution is to buy more memory. The next best solution is to create/increase swap space. | Swap memory isn't as fast as standard memory. So if having memory issues, the proper solution is to buy more memory. The next best solution is to create/increase swap space.<br> | ||
By default, linux will not allocate any hard drive space as swap space. It must be done manually. | By default, linux will not allocate any hard drive space as swap space. It must be done manually. | ||
Line 7: | Line 7: | ||
* <code>swapon --show</code> | * <code>swapon --show</code> | ||
If this command returns no output, then there is no swap space allocated. | If this command returns no output, then there is no swap space allocated. | ||
<br><br> | |||
Alternatively, to view all allocated memory (including swap space), run the command: | Alternatively, to view all allocated memory (including swap space), run the command: | ||
*<code>free -h</code | *<code>free -h</code> | ||
== Allocating Swap Space == | == Allocating Swap Space == | ||
Before allocating swap space, make sure you have enough hard drive space for what you want to allocate. See [[Linux Hard Drive Management]] for details. | Before allocating swap space, make sure you have enough hard drive space for what you want to allocate. See [[Linux Hard Drive Management]] for details. | ||
{{ Note | If you want to adjust already existing swap space, then first use <code>swapoff -a</code> to disable all existing swap space.}} | |||
Create swap space with the command: | Create swap space with the command: | ||
* <code>sudo fallocate -l <size_to_allocate> /swapfile</code> | * <code>sudo fallocate -l <size_to_allocate> /swapfile</code> | ||
** <size_to_allocate> should be <code>1G</code> to allocate 1 gig, <code>2G</code> to allocate 2 gigs, etc. | ** <size_to_allocate> should be <code>1G</code> to allocate 1 gig, <code>2G</code> to allocate 2 gigs, etc. | ||
<br> | |||
Make sure to set it to be only root-accessible with: | Make sure to set it to be only root-accessible with: | ||
* <code>sudo chmod 600 /swapfile</code> | * <code>sudo chmod 600 /swapfile</code> | ||
<br> | |||
Finally, flag this as swap space with: | Finally, flag this as swap space with: | ||
* <code>sudo mkswap /swapfile</code> | * <code>sudo mkswap /swapfile</code> | ||
Line 30: | Line 32: | ||
* <code>swapon /swapfile</code> | * <code>swapon /swapfile</code> | ||
Note that this command will only work until you restart the system. But you can now use a command to view available swap space, to verify that it allocated correctly. | Note that this command will only work until you restart the system. But you can now use a command to view available swap space, to verify that it allocated correctly. | ||
<br><br> | |||
If the swap space allocated correctly, we can now make it permanent by editing <code>/etc/fstab</code> and adding the line: | If the swap space allocated correctly, we can now make it permanent by editing <code>/etc/fstab</code> and adding the line: | ||
* <code>/swapfile swap swap defaults 0 0</code> | * <code>/swapfile swap swap defaults 0 0</code> | ||
Line 36: | Line 38: | ||
== Adjusting the Swappiness Property == | == Adjusting the Swappiness Property == | ||
'''Swappiness''' defines how often the system attempts to swap data out of RAM and into the swap space. This value ranges between 0-100, and read in by the system as a percentage. | |||
Closer to 0 means that the system will swap to disk less often, unless necessary. | * Closer to 0 means that the system will swap to disk less often, unless necessary. | ||
Closer to 100 means that the system will attempt to swap more often. | * Closer to 100 means that the system will attempt to swap more often. | ||
Using swap space is expensive compared to normal memory, so you probably want this value to be low in cases where performance is important (such as server applications). | Using swap space is expensive compared to normal memory, so you probably want this value to be low in cases where performance is important (such as server applications). | ||
<br><br> | |||
You can view the current swappiness value with the below commands. The default is likely around 60. | You can view the current swappiness value with the below commands. The default is likely around 60. | ||
* '''Older Location''' (May not exist on newer systems): <code>cat /proc/sys/vm/swappiness</code> | * '''Older Location''' (May not exist on newer systems): | ||
* '''Newer Location''' (May not exist on older systems): <code>cat /sys/fs/cgroup/memory/memory.swappiness</code> | ** <code>cat /proc/sys/vm/swappiness</code> | ||
* '''Newer Location''' (May not exist on older systems): | |||
** <code>cat /sys/fs/cgroup/memory/memory.swappiness</code> | |||
<br> | |||
To change current swappiness, edit <code>/etc/sysctl.conf</code> and add the line: | To change current swappiness, edit <code>/etc/sysctl.conf</code> and add the line: | ||
* <code>vm.swappiness=<swap_value></code>, where <code><swap_value><code> is the swap value you desire. | * <code>vm.swappiness=<swap_value></code>, where <code><swap_value></code> is the swap value you desire. | ||
** Ex: <code>vm.swappiness=10</ | ** Ex: <code>vm.swappiness=10</code> |
Latest revision as of 08:53, 15 May 2020
Swap space is a section of hard drive space that is allocated specifically to be used as a memory (RAM) overflow.
Swap memory isn't as fast as standard memory. So if having memory issues, the proper solution is to buy more memory. The next best solution is to create/increase swap space.
By default, linux will not allocate any hard drive space as swap space. It must be done manually.
Viewing Available Swap Space
To specifically view swap space only, run the command:
swapon --show
If this command returns no output, then there is no swap space allocated.
Alternatively, to view all allocated memory (including swap space), run the command:
free -h
Allocating Swap Space
Before allocating swap space, make sure you have enough hard drive space for what you want to allocate. See Linux Hard Drive Management for details.
swapoff -a
to disable all existing swap space.Create swap space with the command:
sudo fallocate -l <size_to_allocate> /swapfile
- <size_to_allocate> should be
1G
to allocate 1 gig,2G
to allocate 2 gigs, etc.
- <size_to_allocate> should be
Make sure to set it to be only root-accessible with:
sudo chmod 600 /swapfile
Finally, flag this as swap space with:
sudo mkswap /swapfile
Activating Swap Space for Use
To test that the swap works, we can temporarily enable it with:
swapon /swapfile
Note that this command will only work until you restart the system. But you can now use a command to view available swap space, to verify that it allocated correctly.
If the swap space allocated correctly, we can now make it permanent by editing /etc/fstab
and adding the line:
/swapfile swap swap defaults 0 0
Adjusting the Swappiness Property
Swappiness defines how often the system attempts to swap data out of RAM and into the swap space. This value ranges between 0-100, and read in by the system as a percentage.
- Closer to 0 means that the system will swap to disk less often, unless necessary.
- Closer to 100 means that the system will attempt to swap more often.
Using swap space is expensive compared to normal memory, so you probably want this value to be low in cases where performance is important (such as server applications).
You can view the current swappiness value with the below commands. The default is likely around 60.
- Older Location (May not exist on newer systems):
cat /proc/sys/vm/swappiness
- Newer Location (May not exist on older systems):
cat /sys/fs/cgroup/memory/memory.swappiness
To change current swappiness, edit /etc/sysctl.conf
and add the line:
vm.swappiness=<swap_value>
, where<swap_value>
is the swap value you desire.- Ex:
vm.swappiness=10
- Ex: