This topic was published by DevynCJohnson and viewed 1918 times since "". The last page revision was "".
- Topics - 437
All Linux users have likely heard of swap. The majority of us have made a swap partition, and a few of us have made swap files. However, many Linux users (especially newbies) do not fully understand swap. The swap space is very important, so it is beneficial to understand this special entity.
The swap is typically its own partition, but it may sometimes be a file. The swap partition is a whole partition formatted as "swap". Most Linux documentation recommends that the swap partition be the size of twice the system's RAM. To clarify, if the system uses 4GB of RAM/memory, then the swap should be 8GB. This may sound like too much, but this helps many users and may be needed. The minimum recommended amount is equal to half the size of the RAM (if RAM=4GB, then swap=2GB). The swap file is rarely used or heard about. This form of swap is only used in special cases. Again, the swap file is made to be twice the size of the RAM. Both the swap file and partition can co-exist and be used with the same active system, although users choose one over the other.
NOTE: The terms "swap" and "swap space" can be used interchangeably. The term "swap partition" only refers to a partition dedicated to swap, and a "swap file" is a file used as swap.
For swap to be used by so many systems, it must be very important. So, what does it do? Well, the swap is equivalent to the paging file used on Windows systems. When the RAM gets too full, the Linux kernel will move some data from memory to the swap space. The data will remain on swap until needed. When a program requests data that is being stored on swap, the kernel will move the data back to the memory. The program never knows anything about swap.
The amount of physical memory (RAM) plus swap is the total amount of "virtual memory". However, treat your system as if it only has physical memory. To explain, if you have a system with 4GB of RAM, but you need 6GB of RAM, you cannot simply use swap to satisfy that need. This is because swap is much slower than RAM and swap is meant to store data you are not actively using. For example, if a user wanted to install a video game that requires 6GB of RAM on a system with 4GB of physical memory, it would not be possible. The game would need access to its data on RAM. Waiting for the kernel to move data to and from swap is not practical. This would cause severe lag (if you could force the game to install). Plus, when installing a program that specifically checks the amount of RAM, the game would see that the system does not satisfy its needs.
The swap is required for supporting "hibernation" (known as suspending to disk). Some people might hibernate their system instead of shutting it down or suspending (the data is kept on the RAM). Large amounts of swap are needed to hibernate the system, hence the reason for making so much swap. In hibernation, the data on RAM is dumped in the swap space. This allows the hardware to use less power than suspension. When suspending a system, the data is kept on the RAM, so the system cannot power down as well as it can in hibernation.
As of Linux kernel 3.11, the kernel now supports zswap. Zswap is a special feature that allows data to be compressed on the RAM instead of moving to swap. The purpose is to reduce the usage of swap, thus reducing IO functions. This concept is faster than using swap because the hard-drive (and other types of storage) is slower than the RAM. The compression used by zswap is LZO.
Swap files are alternatives to swap partitions, although swap partitions are used more often than swap files. Swap files are useful on systems that temporarily need more swap or on systems that need to change the amount of available swap. The latter is useful on embedded systems, and the former is seen on some busy servers. Swap files are just as fast/efficient as swap partitions. This is possible because the Linux kernel bypasses the filesystem's overhead when writing to swap files (thanks to detailed mapping). However, Btrfs does not support swap files. Swap partitions are preferred over the swap files due to security purposes. It is easier to steal private data from a swap file than a swap partition.
A few commands are available to allow the user to view information about the swap. Typing "swapon -s" displays the location of the swap space, its type (partition or file), total size, amount used, and priority (discussed later). The command "free -m", displays information about the swap and RAM. Users could use "fdisk -l" (with Root privileges) to view some swap information. However, since some Linux users use the GPT partitioning-table/disk-label, that last command will not work.
NOTE: Any command that changes or configures swap must be run with root privileges.
Swap partitions can be made using various GUI-based partition editors like GParted. Also, when installing a Linux distro, the installation process creates a swap space or prompts the user to configuring the swap via the installer's partition manager. However, it is still important to understand how to create a swap partition using the command-line. First, find a partition or an unpartitioned portion of your storage device. Remember to move any important files off the partition that is planned to be swap space or the data will be lost. Then, execute "mkswap /dev/XYZ", where "XYZ" designates the device and partition number (like sda2, sdb4, etc.). Next, type "swapon /dev/XYZ" (type the same partition) to tell the system to use the swap space. Now, if you want this swap space to be used every time the system boots, then add a line like
/dev/sda2 none swap defaults 0 0to /etc/fstab. The line tells the kernel to mount the partition and use the swap.
NOTE: When using fdisk or cfdisk to format a partition to swap, keep in mind that swap is designated as "82".
Alternately, users can create a swap file. To do so, type "
fallocate -l 512M /PATH/swapfile" to make a 512 megabyte file called "swapfile" in the designated path (use "G" for gigabytes). Users may name the swap file whatever they wish. Some users use a file extension like "*.swp" or "*.swap", but a file extension is not required. Next, type "chmod 600 /path/swapfile" which will set the permissions to only allow Root access. This is essential for security reasons. Then, type "mkswap /path/swapfile" to format the file as swap. After that, type "swapon /path/swapfile" to use the swap and add "/path/swapfile none swap defaults 0 0" to /etc/fstab to use the swap file each time after booting.
There is a second way to create a swap file. Type
dd if=/dev/zero of=/path/swapfile bs=1024 count=SIZEwhere "SIZE" is the desired size in bytes (1000000000 = ~1GB) and "/path/swapfile" is where the swap file should be placed and named. Then, perform mkswap and swapon and add the swap file to /etc/fstab (if desired).
NOTE: The "if=/dev/zero" statement fills the swap file with zeros, and "bs=1024" sets the block size.
To delete a swap file, you must follow these instructions carefully. First, run "swapoff -a" which will tell the kernel to get its data out of swap and then stop using all swap spaces. Next, type
rm -f /path/swapfileto delete the file. After that, if the system has other swap spaces that you want it to continue using, turn swap back on by typing "swapon -a". Remember to remove the swap file entry from /etc/fstab. After all of that, there is no need to reboot.
Users can control how frequently/likely the swap space will be used. The term "swappiness" refers to the likeliness of the system to use swap. This works on a scale of 0-100 where zero means indicates the swap will never be used and 100 is excessive use. Most Linux distros have a default of about "60". The swappiness can be changed temporarily by typing
echo 50 > /proc/sys/vm/swappiness. For a permanent change, add "vm.swappiness=50" to /etc/sysctl.conf or type
Various swap settings can be viewed and changed in the /proc/sys/vm/ directory. Type "
cat /proc/sys/vm/FILE" to view a file and "
echo 'VAL' > /proc/sys/vm/FILE" to change settings temporarily.
If for some reason a user needs the data on swap to be moved from swap back to RAM while keeping the swap on/running, type
swapoff -a && swapon -a. Doing so "resets" the swap. I am not sure why this would be used, but some people ask how to do this. So, I provided an answer.
Remember those priorities I briefly mentioned earlier? These priorities have a purpose. Assume a system has two swap partitions, each on a different hard-drive. One hard-drive is much faster than the other, so the user wants to ensure the faster swap is used before the slower one. To set the priorities, modify the entries in /etc/fstab to look something like below. The priority values are 0 to 32767. In the lines below, the main part to notice is that the "defaults" entries now end with ",pri=X" and no spaces.
/path/swapfile none swap defaults,pri=10 0 0 /dev/sda3 none swap defaults,pri=100 0 0 /dev/sdc5 none swap defaults,pri=1000 0 0
When using "swapon", users can specify the priority to use
swapon -p 300 /dev/sdb2.
If your kernel supports hibernation, but your system will not hibernate, then try this. In /etc/default/grub, replace
[GRUB_CMDLINE_LINUX="resume=UUID=4236dabb-fa7d-4066-b171-91ffa7afb4f4"], then run "update-grub". Next, execute the command string below.
echo "RESUME=UUID=4236dabb-fa7d-4066-b171-91ffa7afb4f4" > /etc/initramfs-tools/conf.d/resume; update-initramfs -u
You may be wondering what is happening. Well, GRUB is being informed which swap space contains the hibernated system (if hibernation took place). Initramfs is also informed about the swap space used for hibernation. Obviously, the user must type their own UUID in-place of mine. The UUID is a special way to specify partitions.
TIP: If you do not like command-lines, then use this command to edit /etc/fstab or other files (gksudo gedit /etc/fstab). Depending on your system, you would use "gksudo", "kdesudo", or some other graphical sudo. Also, you can use any text editor of your choice. GRAPHICAL_SUDO EDITOR /PATH/TO/FILE
- Linux Kernel Slab Allocators - https://dcjtech.info/topic/linux-kernel-slab-allocators/
- ProcFS - https://dcjtech.info/topic/procfs/
- SysFS and ConfigFS - https://dcjtech.info/topic/sysfs-and-configfs/
- Filesystem Types - https://dcjtech.info/topic/filesystem-types/