Swap-Space Management in Operating System

A computer has a sufficient amount of physical memory, but we need more, so we swap some memory on disk most of the time. Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory, which contains process memory images. Whenever our computer runs short of physical memory, it uses its virtual memory and stores information in memory on a disk.

This interchange of data between virtual memory and real memory is called swapping and space on disk as swap space. Swap space helps the computer's operating system pretend that it has more RAM than it actually has. It is also called a swap file.

Virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory on the hard disk, used when RAM is full. Swap space can be useful to computers in the following various ways, such as:

  • It can be used as a single contiguous memory which reduces I/O operations to read or write a file.
  • Applications that are not used or less used can be kept in a swap file.
  • Having sufficient swap files helps the system keep some physical memory free all the time.
  • The space in physical memory that has been freed due to swap space can be used by OS for other important tasks.

Operating systems such as Windows, Linux, etc. systems provide a certain amount of swap space by default which users can change according to their needs. If you don't want to use virtual memory, you can easily disable it together. Still, if you run out of memory, then the kernel will kill some of the processes to create a sufficient amount of space in physical memory so that it totally depends upon the user whether he wants to use swap space or not.

What is Swap-Space Management?

Swap-space management is another low-level task of the operating system. Virtual memory uses disk space as an extension of main memory. Since disk access is much slower than memory access, using swap space significantly decreases system performance. The main goal for the design and implementation of swap space is to provide the best throughput for the virtual memory system.

Swap space is used in various ways by different operating systems, depending on the memory-management algorithms in use. For example, systems that implement swapping may use swap space to hold an entire process image, including the code and data segments. Paging systems may simply store pages that have been pushed out of the main memory. The amount of swap space needed on a system can vary depending on the amount of physical memory, the amount of virtual memory it is backing, and how it is used. It can range from a few megabytes of disk space to gigabytes.

Note that it may be safer to overestimate than to underestimate the amount of swap space required because if a system runs out of swap space, it may be forced to abort processes or may crash entirely. Overestimation wastes disk space that could otherwise be used for files, but it does no other harm. Some systems recommend the amount to be set aside for swap space. Solaris, for example, suggests setting swap space equal to the amount by which virtual memory exceeds page-able physical memory. Previously, Linux suggested setting swap space to double the amount of physical memory, although most Linux systems now use considerably less swap space. There is currently much debate in the Linux community about whether to set aside swap space at all.

Some operating systems, including Linux, allow multiple swap spaces. These swap spaces are usually put on separate disks so the load placed on the I/O system by paging and swapping can be spread over the system's I/O devices.

Uses of Swap Space

The different operating system uses Swap-space in various ways. The systems that are implementing swapping may use swap space to hold the entire process, including image, code, and data segments.

  • Swapping is a memory management technique used in multi-programming to increase the number of processes sharing the CPU. It is a technique of removing a process from the main memory, storing it into secondary memory, and then bringing it back into the main memory for continued execution. This action of moving a process out from main memory to secondary memory is called Swap Out. The action of moving a process out from secondary memory to main memory is called Swap In.
  • Paging systems may simply store pages that have been pushed out of the main memory. The need for swap space on a system can vary from megabytes to gigabytes. Still, it also depends on the amount of physical memory, the virtual memory it is backing, and how it uses the virtual memory.

It is safer to overestimate than to underestimate the amount of swap space required because if a system runs out of swap space, it may be forced to abort the processes or may crash entirely. Overestimation wastes disk space that could be used for files, but it does not harm others. The following table shows different systems using the amount of swap space:

S.No.SystemSwap Space
1.SolarisSwap space is equal to the amount of physical memory.
2.LinuxSwap space is double the amount of physical memory.

Solaris setting swap space equal to the amount by which virtual memory exceeds page-able physical memory. Previously, Linux has suggested setting swap space to double the amount of physical memory. Today, this limitation is gone, and most Linux systems use considerably less swap space.

Including Linux, some operating systems allow the use of multiple swap spaces, including both files and dedicated swap partitions. The swap spaces are placed on the disk, so the load on the I/O by the paging and swapping will spread over the system's bandwidth.

Where does the Swap Space Reside?

Swap space can reside in one of these two places: The normal file system or Separate disk partition.

Swap-Space Management in Operating System

If the swap space is simply a large file within the file system, normal file-system routines can be used to create, name, and allocate its space. This approach, though easy to implement, is inefficient. Navigating the directory structure and a disk-allocation data structure takes extra disk access.

  • External fragmentation can greatly increase swapping times by forcing multiple seeks during the reading or writing of a process image. We can improve performance by caching the block location information in physical memory and by using special tools to allocate physically contiguous blocks for the swap file. However, the cost of traversing the file-system data structures still remains.
  • Alternatively, swap space can be created in a separate raw partition, as no file system or directory structure is placed in this space. Rather, a separate swap-space storage manager is used to allocate and deallocate the blocks from the raw partition. This manager uses algorithms optimized for speed rather than storage efficiency because swap space is accessed more frequently than file systems.
  • Internal fragmentation may increase, but this trade-off is acceptable because the life of data in the swap space generally is much shorter than that of files in the file system. Swap space is reinitialized at boot time, so any fragmentation is short-lived. This approach creates a fixed amount of swap space during disk partitioning. Adding more swap space requires repartitioning the disk or adding another swap space elsewhere.

Some operating systems are flexible and can swap both in raw partitions and file-system space, such as Linux. The policy and implementation are separate, allowing the machine's administrator to decide which type of swapping to use. The trade-off is between the convenience of allocation and management in the file system and swapping performance in raw partitions.

Allocation of Swap Space

Management appliances allocate a specific amount of swap space based on the size of the appliance's memory and are restricted by the amount of available disk space.

The amount of swap space that a virtual machine requires is double the size of its RAM. This amount provides enough space for data that is no longer required by RAM to be placed in the swap space. In addition, there is enough space for emergencies where RAM needs to be cleared due to hypervisor demands.

The Management appliance cannot allocate all of the space from a data disk to swap space, as space is required for databases. The Management appliance follows a specific set of conditions when it allocates swap space. These conditions are as follows:

  1. The maximum amount of work-in-progress partition that can be dedicated to swap space is 25%.
  2. When the Management appliance increases the swap space size, the swap size does not increase by more than 20% at a single instance. The maximum increase of 20% in one instance enables Informix and analytics to allocate their disk spaces during the boot process.
  3. You can allocate more data disk space by adding a new data disk to a Management appliance.
  4. You can increase the size of your code disk by following the procedure of increasing code disk size for appliances.

Example of Swap Space Management

The traditional UNIX kernel started with swapping that copied the entire process between contiguous disk regions and memory. UNIX later evolved to a combination of swapping and paging as paging hardware became available.

In Solaris 1, the designers changed standard UNIX methods to improve efficiency.

  • When a process executes, text-segment pages containing code are brought in from the file system, accessed in main memory, and thrown away if selected for page out.
  • It is more efficient to reread a page from the file system than write it to swap space and then reread it.
  • Swap space is only used as a backing store for anonymous memory pages, including memory allocated for the stack, heap, and uninitialized process data.

More changes were made in later versions of Solaris. The biggest change is that Solaris now allocates swap space only when a page is forced out of physical memory rather than when the virtual memory page is first created. This scheme gives better performance on modern computers, which have more physical memory than older systems.

Linux is almost similar to the Solaris system. The swap space is used only for anonymous memory or for regions of memory shared by several processes in both systems. In the Linux system, one or more swap areas can be established.

Swap-Space Management in Operating System
  • A swap area may be in a swap file on a regular file system or a dedicated file partition.
  • Each swap area consists of 4-KB page slots used to hold the swapped pages.
  • An array of integers counters associated with each swap area is a swap-map corresponding to a page slot in the swap area.
  • If the value of a counter is 0, the corresponding page slot is available.
  • If the values are greater than 0, it indicates that a swapped page occupies the page slot.
  • The value of the counter indicates the number of mappings to the swapped page. For example, a value of 3 indicates that the swapped page is mapped to the 3 different processes.





Latest Courses