:''Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.''

:''Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.''

+

== Swap space ==

Swap space will usually be a disk partition but can also be a file. Users may create a swap space during installation of Arch Linux or at any later time should it become necessary. Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).

Swap space will usually be a disk partition but can also be a file. Users may create a swap space during installation of Arch Linux or at any later time should it become necessary. Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).

+

+

To check swap status, use:

+

$ swapon -s

+

+

Or：

+

$ free -m

+

+

{{Note|There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.}}

== Swap partition ==

== Swap partition ==

−

A swap partition can be created with most GNU/Linux partitioning tools (e.g. {{Codeline|fdisk}}, {{Codeline|cfdisk}}). Swap partitions are designated as type '''82'''.

+

A swap partition can be created with most GNU/Linux partitioning tools (e.g. {{Ic|fdisk}}, {{Ic|cfdisk}}). Swap partitions are designated as type '''82'''.

−

To set up a Linux swap area, the {{Codeline|mkswap}} command is used. For example:

+

To set up a Linux swap area, the {{Ic|mkswap}} command is used. For example:

# mkswap /dev/sda2

# mkswap /dev/sda2

Line 28:

Line 41:

To enable this swap partition on boot, add an entry to [[fstab]]:

To enable this swap partition on boot, add an entry to [[fstab]]:

/dev/sda2 none swap defaults 0 0

/dev/sda2 none swap defaults 0 0

+

+

{{Note|If using a TRIM supported SSD, discard is a valid mount option for swap. If creating swap manually, using -d or --discard achieves the same. For more information and other available mount options, see the swapon man page.}}

== Swap file ==

== Swap file ==

−

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

+

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

+

+

{{Note|The BTRFS filesystem does not currently support swapfiles. Failure to heed this warning may result in filesystem corruption.}}

=== Swap file creation ===

=== Swap file creation ===

−

As root use {{Codeline|fallocate}} to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) ({{Codeline|dd}} can also be used but will take longer). For example, creating a 512 MB swap file:

+

As root use {{Ic|fallocate}} to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) ({{Ic|dd}} can also be used but will take longer). For example, creating a 512 MB swap file:

# fallocate -l 512M /swapfile

# fallocate -l 512M /swapfile

+

Or

# dd if=/dev/zero of=/swapfile bs=1M count=512

# dd if=/dev/zero of=/swapfile bs=1M count=512

Line 52:

Line 70:

# swapon /swapfile

# swapon /swapfile

−

Edit {{Filename|/etc/fstab}} and add an entry for the swap file:

+

Edit {{ic|/etc/fstab}} and add an entry for the swap file:

/swapfile none swap defaults 0 0

/swapfile none swap defaults 0 0

−

Ensure proper swap file size and activation with {{Codeline|free}}:

+

=== Remove swap file ===

−

−

$ free -m

−

−

=== Swap file removal ===

To remove a swap file, the current swap file must be turned off.

To remove a swap file, the current swap file must be turned off.

As root:

As root:

+

# swapoff -a

# swapoff -a

Remove swapfile:

Remove swapfile:

+

# rm -rf /swapfile

# rm -rf /swapfile

−

=== Resume from Swap file ===

+

=== Swap file resuming ===

−

Resuming the system from a swap file after hibernation requires an addition parameter to the kernel compared to resuming from a swap partition. The additional parameter is '''resume_offset=<Swap File Offset>'''.

+

Resuming the system from a swap file after hibernation requires an addition [[kernel parameters|kernel parameter]] compared to resuming from a swap partition. The additional parameter is {{ic|1=resume_offset=<Swap File Offset>}}.

−

The value of '''<Swap FIle Offset>''' can be obtained from the output of filefrag -v; The output is in a table format; the required value is located in the 'physical' column from the first row. Eg:

+

The value of {{ic|<Swap File Offset>}} can be obtained from the output of {{ic|filefrag -v}}; The output is in a table format; the required value is located in the {{ic|physical}} column from the first row. Eg:

# filefrag -v /swapfile

# filefrag -v /swapfile

Filesystem type is: ef53

Filesystem type is: ef53

Line 84:

Line 100:

...

...

−

From the example <Swap FIle Offset> is 7546880.

+

From the example {{ic|<Swap FIle Offset>}} is {{ic|7546880}}.

+

{{Note|Please note that in kernel {{ic|resume}} parameter you still have to type path to partition (e.g. {{ic|1=resume=/dev/sda1}}) not to swapfile explicitly! Parameter {{ic|resume_offset}} is for informing system where swapfile starts on hard disk (e.g. {{ic|1=resume_offset=7546880}}).}}

−

== Swap over USB ==

+

== Swap with USB device ==

−

Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. To add a a USB device to SWAP:

+

Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. If you have a very full hard disk, USB device can be used as partition temporally. But this method has some severe disadvantage：

+

* USB device is slower than hard disk.

+

* flash memories have limited write cycles. Using it as swap partition will kill it quickly.

+

* when another device is attached to the computer, no swap can be used.

−

First, take a USB flash and partition it with a swap partition. To do this you can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table.

+

To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table.

{{Box RED||Make sure you are writing the partition to the correct disk!}}

{{Box RED||Make sure you are writing the partition to the correct disk!}}

−

Next edit the {{Codeline|fstab}}

+

Next edit the {{Ic|fstab}}

# nano /etc/fstab

# nano /etc/fstab

Line 105:

Line 125:

ls -l /dev/disk/by-uuid/ | grep /dev/sdc1

ls -l /dev/disk/by-uuid/ | grep /dev/sdc1

−

Just replace sdc1 with your new USB swap partition. {{Codeline|sdb1}}

+

Just replace sdc1 with your new USB swap partition. {{Ic|sdb1}}

{{Box GREEN||We use UUID because when you attach other devices to the computer it could modify the device order}}

{{Box GREEN||We use UUID because when you attach other devices to the computer it could modify the device order}}

Line 115:

Line 135:

in the ''original'' swap entry for teaching fstab to use HD swap only when USB is full

in the ''original'' swap entry for teaching fstab to use HD swap only when USB is full

−

ADVANTAGES

+

This guide will work for other memory such as SD cards, etc.

−

* access time is lower than traditional HD

+

== Performance Tuning ==

−

* avoid power on HD when is in stand-by

−

* USB pendrives are cheaper than HD (meaning: you can't get a $5 HDD, price per gigabyte is obviously much higher for flash drives)

−

DISADVANTAGES

+

Swap values can be adjusted to help performance.

−

−

* flash memories have limited write cycles

−

* another device attached to the computer

−

−

To check if the tweak works, run

−

−

swapon -s

−

−

and see if your new swap partition is listed.

−

−

Naturally this guide will work for other memory such as SD cards, etc.

−

−

== Performance Tuning ==

=== Swappiness ===

=== Swappiness ===

Line 140:

Line 145:

The ''swappiness'' [[sysctl]] parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.

The ''swappiness'' [[sysctl]] parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.

−

{{File

+

{{hc|/etc/sysctl.conf

−

|name=/etc/sysctl.conf

+

|2=<nowiki>

−

|content=<nowiki>

vm.swappiness=1

vm.swappiness=1

vm.vfs_cache_pressure=50

vm.vfs_cache_pressure=50

Line 149:

Line 153:

=== Priority ===

=== Priority ===

−

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ({{Filename|/dev/sda}}) and a slower disk ({{Filename|/dev/sdb}}), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the {{Codeline|1=pri}} parameter:

+

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ({{ic|/dev/sda}}) and a slower disk ({{ic|/dev/sdb}}), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the {{Ic|1=pri}} parameter:

/dev/sda1 none swap defaults,pri=100 0 0

/dev/sda1 none swap defaults,pri=100 0 0

/dev/sdb2 none swap defaults,pri=10 0 0

/dev/sdb2 none swap defaults,pri=10 0 0

−

Or via the {{Codeline|−p}} (or {{Codeline|−−priority}}) parameter of swapon:

+

Or via the {{Ic|−p}} (or {{Ic|−−priority}}) parameter of swapon:

# swapon -p 100 /dev/sda1

# swapon -p 100 /dev/sda1

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

Revision as of 18:36, 1 February 2013

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Contents

Swap space

Swap space will usually be a disk partition but can also be a file. Users may create a swap space during installation of Arch Linux or at any later time should it become necessary. Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).

To check swap status, use:

$ swapon -s

Or：

$ free -m

Note: There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.

Swap partition

A swap partition can be created with most GNU/Linux partitioning tools (e.g. fdisk, cfdisk). Swap partitions are designated as type 82.

Note: If using a TRIM supported SSD, discard is a valid mount option for swap. If creating swap manually, using -d or --discard achieves the same. For more information and other available mount options, see the swapon man page.

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Note: The BTRFS filesystem does not currently support swapfiles. Failure to heed this warning may result in filesystem corruption.

Swap file creation

As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer). For example, creating a 512 MB swap file:

Note: Please note that in kernel resume parameter you still have to type path to partition (e.g. resume=/dev/sda1) not to swapfile explicitly! Parameter resume_offset is for informing system where swapfile starts on hard disk (e.g. resume_offset=7546880).

Swap with USB device

Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. If you have a very full hard disk, USB device can be used as partition temporally. But this method has some severe disadvantage：

USB device is slower than hard disk.

flash memories have limited write cycles. Using it as swap partition will kill it quickly.

when another device is attached to the computer, no swap can be used.

To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table.

Make sure you are writing the partition to the correct disk!

Next edit the fstab

# nano /etc/fstab

Now add a new entry, just under the current swap entry, which take the current swap partition over the new USB one

UUID=... none swap defaults,pri=10 0 0

where UUID is taken from the output of the command

ls -l /dev/disk/by-uuid/ | grep /dev/sdc1

Just replace sdc1 with your new USB swap partition. sdb1

We use UUID because when you attach other devices to the computer it could modify the device order

Last, add

pri=0

in the original swap entry for teaching fstab to use HD swap only when USB is full

This guide will work for other memory such as SD cards, etc.

Performance Tuning

Swap values can be adjusted to help performance.

Swappiness

The swappinesssysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.

/etc/sysctl.conf

vm.swappiness=1
vm.vfs_cache_pressure=50

Priority

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk (/dev/sda) and a slower disk (/dev/sdb), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the pri parameter: