from Wikipedia, the free encyclopedia

In computer science , swapping [ swɐpɪŋ ] (English for rearrangement ; from English to swap , 'to exchange') describes the transfer of data from a higher to a lower level or the opposite loading within the storage hierarchy . Through swapping, the advantages of higher levels, i.e. higher speed, and lower levels, namely higher capacity and more cost-effective storage, are to be made usable almost simultaneously. This is paid for by the time required to swap. Usually the term refers to the movement of data between memory and hard drive .

Working method

Storage and retrieval of complete processes

If there is no more free memory available to service a request, data that is not required is swapped out to make space in the memory. If they are needed again, they have to be reloaded (and something else has to give way). The software routine used for this purpose and working in the background is also referred to as a swapper . Swapping was initially combined with segmentation , a special type of memory management in operating systems . Swapping already existed before paging , which is used by most modern operating systems as the basis for virtual memory management . Swapping and paging are mostly combined with one another today.

Swapping often takes place when the scheduler activates another process , since the other process now needs its data, while the previous one is no longer accessed.

There are two problems to be solved with swapping:

  1. The memory addresses of the local variables can change between swapping out and reading in again if the size of the memory segment changes. In the case of memory management using segmentation, this is circumvented by relocating the variables in that the memory addresses of the variables are recalculated each time they are read. In paging, this is done by mapping virtual addresses to real addresses using the page table .
  2. The memory protection of the managing operating system, which prevents programs from leaving the memory area assigned to them for reading or writing.

Both problems can also be solved by combining swapping with virtual memory management .

In the case of operating systems that only support segmentation, all of the swapped-out memory segments form the swap file in their entirety , while in the case of operating systems that support paging, all the swapped-out memory pages form the swap-out file.

Swapping versus paging

Virtual memory and hard disk caching can in principle be processed using the same mechanism, which modern operating systems typically support. An open file can be shown as part of the address space - only those areas that are actually accessed by the application are actually read in (“swap-in”). Conversely, “save” simply becomes a “swap out” process.

Sometimes it is linguistically defined that "swap in" and "swap out" would only refer to the transfer between the main memory and the dedicated swap space, but not to the transfer of other file contents, whereas "page in" would refer to any transfer of a file into the main memory, "page out" denotes an (arbitrary) transfer from the main memory to the file system.


  • A program with a memory leak uses more memory than is physically available to the system. The system has a swap file and some memory pages of the program are swapped out there. This is called swapping, since the swap file is the only file involved.
  • A program opens a file without using the direct input and output option. The file is loaded into the file system cache . This is called page in because the swap space is not affected.