swap file (swap space or page file)
What is a swap file (swap space or page file)?
A swap file is a system file that creates temporary storage space on a solid-state drive or hard disk when the system runs low on memory. The file swaps a section of RAM storage from an idle program and frees up memory for other programs.
By using a swap file, the computer can use more memory than is physically installed. In other words, it can run more programs than it could run with just the limited resources of the installed RAM.
Swap files are not stored in physical RAM, which is why they are a type of virtual memory. Having a swap file allows a computer's operating system (OS) to pretend that it has more RAM than it actually does. The least recently used files or programs in RAM can be "swapped out" to the hard disk until they're needed later, allowing new files or programs to be "swapped in" to RAM.
The implementation of swap files varies with the OS, which takes care of creating and using a swap file as required. It also releases the drive space when it is no longer required by a program.
A swap file is also known as a page file, swap space or paging file. It is hidden on the system by default.
Why swap files are important
Swap files play two important roles in computer processing:
They can handle the extra load of memory consumption. Some applications consume a huge amount of RAM, depriving other background applications that also need memory. Swap files help resolve this situation by paging out idle files to provide extra virtual memory.
Unforeseen circumstances. When a particular program consumes extra memory, or when extra space is needed for device operations, swap files provide some breathing room until the user can come up with a more permanent solution, such as a RAM upgrade.
Benefits and drawbacks of swap files
While swap files help extend physical memory and thus make it possible to run more programs, they can also lead to system slowdowns. The file swaps out idle programs for in-use programs multiple times, which causes a lot of reading and writing on the system drive.
When the system starts using the swap file on the drive, performance may suffer because SSDs and hard disk drives (HDD) are much slower than RAM.
Temporary and permanent swap files
Swap files can be permanent or temporary. Operating systems such as Windows can be configured to use temporary swap files, while others such as Unix and Linux can set aside a permanent swap space to create a permanent swap file.
Temporary swap files usually use fragmented hard disk space. They don't permanently reserve part of the system hard disk, so they're more useful if disk space is limited.
Permanent swap files take a contiguous section of a hard disk. By organizing the swap file in a single contiguous space, fewer I/O operations are required to read or write a complete file, so it doesn't affect system speed or performance as much as a temporary swap file. However, permanent swap files also take up more disk space, so if this space is already limited, they could slow down the system.
How to manage swap files in Windows 10
In Windows 10, swap files or page files are hidden with the .sys extension stored on the system drive. The swapping process begins automatically whenever a user opens more applications than the RAM can accommodate.
Usually, the minimum swap file size is 1.5 times the RAM capacity and the maximum is 4 times the RAM. For example:
RAM = 1 gigabyte (GB)
Minimum swap file = 1.5 GB
Maximum swap file = 4 GB
Windows 10 automatically manages the swap file according to the system configuration and RAM. However, a user can also manually adjust the swap file size if performance issues arise.
The steps for manually adjusting Windows 10 swap files are:
- Right click on the Start icon in the system tray and click on System. (Note: The system administrator account should be used to log into Windows 10.)
- Navigate to Advanced system settings and click on the Advanced
- Click on the Settings button in the Performance section and navigate to the Advanced
- Click the Change button and uncheck the Automatically manage paging file size for all drives checkbox
- Select the drive where Windows 10 is installed.
- Manually enter the maximum and minimum swap file size according to the physical memory present on the system.
- Click the Set button and then the OK button.
- Restart the computer.
Swap files in Linux
Linux has two types of swap space. One is the swap partition, a dedicated section or partition on the drive. The second is the swap file that resides among system and data files and is easy to resize.
With a swap file, a separate partition is not required. Instead, the user simply creates a file under the root, which the system then uses as the swap space.
To verify the swap space already available on a Linux system, two commands can be used, either free or swapon:
free command
Input: $ sudo free –h
Output:
|
total |
used |
free |
shared |
buff/cache |
available |
Mem: |
488M |
158M |
80M |
2.1M |
246M |
220M |
Swap: |
1.0G |
506M |
520M |
|
|
|
If there's no swap space on the system, the output of the free command could look like this:
|
total |
used |
free |
shared |
buff/cache |
available |
Mem: |
488M |
158M |
80M |
2.1M |
246M |
220M |
Swap: |
0G |
0G |
0G |
|
|
|
The free command shows the size of the swap space. However, it doesn't specify if the space is a swap partition or a swap file. For this, the swapon command is more useful
swapon command
Input: $ sudo swapon –show
Output:
NAME |
TYPE |
SIZE |
USED |
PRIO |
/swapfile |
file |
1024M |
500M |
-1 |
The swapon command cannot show any output if the system doesn't have any swap space.
Creating a swap file and making it available in Linux
If the existing swap space is inadequate for system operations, a new swap file can be created in Linux. The steps involved are:
- Create a swap file with the mkfile command and specify the size of the swap file in k (kilobytes), b (blocks) or m (megabytes).
- Activate the swap file by using the swap command.
- Use the absolute path name to specify the swap file.
- To ensure that the swap file is activated automatically when the system boots, specify the entry of swap file to the /etc/vfstab.
- Finally, verify if the swap file is added.
A swap file in Linux can be created without root permissions. But to avoid accidental overwriting, the root should be the owner of the swap file.
Removing a swap file in Linux
If a Linux swap file is no longer needed, it can be removed using these steps with superuser access:
- Remove the swap space to remove the swap file name. This way, the file will no longer be available for swapping, although it will not be deleted.
- Edit the /etc/vfstab file to delete the swap file entry.
- Recover the disk space so it will be available for other use.
- Verify if the swap file is removed and no longer available.
Learn some tips to improve Windows 10 performance and check out the differences between Windows 10 and Windows 11. Also, explore over 80 useful Linux commands, read up on how to optimize Linux swap space usage and find out how memory compression compares to page swapping.