Swap Space

I want to read about swap space because it helped me solve a problem that I had on an EC2 server.

Date Created:

References



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.

Recommended System Swap Space

  • 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:
  1. Create a new swap partition
  1. Create a new swap file
  1. 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:

  1. Remove the entire volume used for swap
  2. Remove a swap file
  3. reduce swap space

Moving Swap Space


To move swap space from one location to another:

  1. Remove swap space.
  2. 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:

  1. 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.
  2. 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.


Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


Insert Chart

ESC

View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language