Swap Space
I want to read about swap space because it helped me solve a problem that I had on an EC2 server.
References
- Swap Space Redhat Documentation
- Digital Ocean How to Add Swap Space on Debian 11 *
- Lenovo Swap file Reference
- How to Allocate Memory to Work as a swap File in An Amazon Ec2 Instance?
- Linux.com All About Swap Space
- BTRFS Read the Docs Swap File
Notes
Swap Space in Linux is used when the amount of physical memory (RAM) is full If the system needs more memory resources and the RAM is full, inactive pages in memory are move to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.
- Recommended swap space is considered a function of system memory workload, not system memory.
- See table below for recommended swap space.
Failure to allocate sufficient swap space on these systems can cause issues such as instability or even render the installed system unbootable.
- Distributing swap space over multiple storage devices improves swap space performance, particularly on systems with fast drives, controllers, and interfaces.
Adding Swap Space
- It might be advantageous to increase the amount of swap space to 4GB if you perform memory-intense operations or run applications that require a large amount of memory.
- You have three options when creating swap space:
- Create a new swap partition
- Create a new swap file
- Extend swap on an existing LVM2 logical volume (Recommended)
Removing Swap Space
Sometimes it may be prudent to reduce swap space after installation - for example, after downgrading RAM. You have three options:
- Remove the entire volume used for swap
- Remove a swap file
- reduce swap space
Moving Swap Space
To move swap space from one location to another:
- Remove swap space.
- Add swap space.
What is a swap file?
A swap file is a file on your computer's hard drive that is used as virtual memory. It is an extension of the computer's physical memory random-access memory (AM) and acts as a temporary storage space for data that doesn't fit in the RAM. When your system's memory is running low, the operating system moves less frequently used data from the RAM to the swap file, making room for more important data in the RAM.
A swap file is necessary because it allows your computer to handle more data than it can fit into the physical random-access memory (RAM). When your RAM is filles with active programs and data, the operating system needs to free up space to accommodate new data. The swap file provides extra space by temporarily storing data that is not immediately needed, which helps prevent your system from crashing due to insufficient memory.
When your computer's random-access memory (RAM) is close to reaching its maximum capacity, the operating system identifies the least used portions of memory and transfers them to the swap file. This process is known as swapping out.
When data from the swap file is needed again, the operating system swaps it back into the RAM from the file, a process called swapping in.
On Linux, you can use the free
command to see if your system is using the swap file. By default, the swap file is located on the same drive as your operating system. However, you can change its location to a different drive for various reasons. If your system runs out of swap file space and is unable to allocate additional virtual memory, it can lead to crashes or freezing of programs. You can have multiple swap files on your computer.
A swap file and swap partition serve the same purpose, but a swap file is stored on the regular file system and a swap partition is a dedicated partition on your hard drive that is specifically allocated for swap space during the installation of the operating system. You can use a SSD for the swap file, and it can provide better performance compared to a traditional hard disk drive (HDD).
All About Linux Swap Space
Linux divides its physical memory RAM into chunks of memory called pages. Swapping is the process whereby a page of memory is copies to the preconfigures space on the hard disk, called swap space, to free up that page of memory. Swapping is the process whereby a page of memory is copied to the preconfigures space on the hard disk, called swap space, to free up that page of memory.
Swapping is necessary because:
- When the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs memory immediately.
- Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free p the memory for other applications or even for the disk cache.
Swapping does have a downside. Compared to memory, disks are very slow. Memory speeds can be measured in nanoseconds, while disks are measured in milliseconds, so accessing the disk can be tens of thousands times slower than accessing physical memory. The more swapping that occurs, the slower your system will be.
Linux has two forms of swap space: the swap partition and the swap file. The swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. The swap file is a special file in the filesystem that resides amongst your system and data files.
To see what space you have, use the command:
$ swap -s
Filename Type Size Used Priority
/dev/sda5 partition 859436 0 -1
The Type
indicates that this swap space is a partition rather than a file, Size
is listed in kilobytes and Used
tells us how many kilobytes of swap space has been used. Priority
tells Linux which swap space to use first.