All About Linux Swap Part 1: Introduction

In: Linux


29 Jan 2009

Note: This article is the first in a multipart series introducing the Linux swap. Part 1 appears below and is intended to familiarize the reader with the concept of swapping, why it exists, and what it’s used for. Part 2 will highlight basic analysis and management techniques for handling swap space. Part 3 will discuss the current state of swap usage and present opinions about its implementation today.

What is swap?

The first question in an introductory article about swap should probably be, “What is swap?”. But, before that question can be answered, we need to ensure a concrete understanding of a more fundamental concept in computers: memory. If you feel that you could use a brush-up on the different types of memory and their function within a modern computer, check out Memory 101: A Primer.

On the most basic level, swap acts as storage space auxiliary to system memory. Fortunately for developers, modern systems need not know or care about swap. This is achieved by abstracting away the physical details through the use of “virtual memory”, which acts as a sort of mapping system. Of course, memory isn’t the only capacity in which this idea is utilized – quite the opposite. Just about every part of a contemporary general computing system abstracts physical details through various device drivers. Taking it a step further, programming languages themselves disconnect the details of the underlying system from the actual operations in the program. These techniques hide the complexities of where data is stored, and instead allows developers to focus on actually interacting with the device, rather than knowing the nuts-and-bolts of the system. Other common uses include video output and file systems. For example, although a program might be stored at /usr/bin/gcc, the system largely doesn’t care which type of device, drive, platter, or cluster the file exists in, because the details have been dissociated from the functionality.

A discussion should also be had about the term “swapping”. In modern operating systems, swapping and paging have similar meaning and are nearly interchangeable. In the Windows world, the term paging is used almost exclusively, whereas swapping is a preferred term within Unix-like systems. The difference originally was in the amount of data that was changed from memory to disk or vice versa. In more primitive OS’s, swapping originally referred to an entire process being brought in and out of memory. As technology advanced, the need to work on an entire process eventually faded; current systems instead switch out only portions of memory known as a page (a smaller fixed-length set of data). Because the older, traditional method of swapping is not longer utilized, the terms are mostly synonymous and will be for the purpose of these articles.

Using Swap

Although swap acts as an extension of system memory, there are several purposes swap space can serve within that descriptor.

Extra “memory”

In the classic scenario, swap space could be considered a last-resort for data before an application – or the entire system – crashes. How does it work? Let’s pretend you have 1000MB of RAM, of which 800 is used. You now load a program that consumes 300MB of memory. In this simplistic scenario there is more data than memory available; therefore, approximately 100MB will be moved into swap. Is this inherently bad? It isn’t ideal, but typical far better than having the system grind to an uncontrollable halt and forcing a shutdown.

Optimizing memory usage

The operating system may anticipate that data will be repeatedly accessed, in which case it can be very beneficial to keep this information in memory. This is known as cache. If stale program data is sitting in memory, taking up valuable space that can be used for cached data, the kernel may decide to place the infrequently-accessed memory in swap. Probably a common example of this is the cache – or temporary internet files – in a web browser. Especially if you’re browsing around a particular website, there may be some common elements such as the logo that will be displayed continuously. There is no need to fetch the data from the server again, because the file has been placed in a local cache.

This could also be placed into an analogy comparing it to looking up information in a library. You can travel to the library, locate the book, then locate the actual page containing the information (and yes, the analogy holds true for the term “page” discussed above). Do you simply read the passage, close the book, and commute back home, hoping you never need it again? Or is it more efficient to make a copy of that passage and take it with you, just-in-case? Much like you might copy the page and keep it with you for referencing again, so too does a modern system copy a page of data into RAM and keep it there until no longer needed.

Suspend-to-disk (a.k.a. Hibernation)

This is a more esoteric use of swap space, but a convenience of modern OS’s (Linux >= 2.6) – and particularly laptops – the ability to Sleep and Hibernate. Hibernating actually stores the entire contents of memory on non-volatile storage (usually a hard drive), then turns off. Because memory cannot be maintained without power and cached objects can be removed from memory, this makes swap space a prime candidate for the storage location.

Putting it all together

After familiarizing yourself with swap space and its common uses, you might be thinking that the drawbacks sometime outweigh the benefits, and you might be correct. In the desktop age, the much easier fix to using swap as an extension of memory is to simply increase system memory. In the past, this has been an expensive endeavor, but currently, lots of memory can be had for relatively little money. The solution is obvious, easy, and relatively cheap: buy more memory! If you’re at a memory limit due to hardware, software, or financial reasons, fret not, there are some solutions.

The next part of the swap space series, I’ll go into depth on how to analyze and manage your swap, tweaking it for performance and understanding how your system is shuffling data around. Additionally, it will provide examples for how to add more swap space.

  • Share/Bookmark

4 Responses to All About Linux Swap Part 1: Introduction

Avatar

Sheridan Hutchinson

January 31st, 2009 at 8:16 am

Just to make a point of comment, the default in-kernel hibernate doesn’t save the entire contents of memory, it performs a kind of flush and saves just what it deems absolutely necessary.

This is why when you return from a hibernate, even though GNU/linux has returned you to your environment that the hard disk often continues to read things back into cache memory giving the impression of a sluggish machine for a while until things have re-cached.

I believe tux-on-ice is capable/dose save all the cache information as well however it will significantly increase the hibernate and resume from hibernate process.

Avatar

cmdr

February 3rd, 2009 at 7:40 am

Nice article.
Do we realy need swap if we have 2G of memory.

I an using window (shame on me) and it seems that the whole memory is never use. However the system seems to use hard drive all the time.

Avatar

Taylor Gerring

February 3rd, 2009 at 9:14 am

@cmdr

Part 3 will attempt to provide some reasoning for how much swap space is necessary, including an argument for “Is any necessary?”. Check back soon for the final installment of the series and hopefully that will answer your question!

Avatar

Taylor Gerring

February 6th, 2009 at 12:55 pm

@cmdr

Part 3 is now up.

Comment Form

  • Kurt Hitchen: A friend has let me "see" there calendar in Google Calendar, but because their calendar is set to pr [...]
  • bugoy8: https://www.google.com/calendar/dav/nba_13_%4cos+%41ngeles+%4cakers#sports@group.v.calendar.google.c [...]
  • Speaker: Wasn't able to understand how to access the shared folder in the quest OS from this tutorial. Hmmm [...]
  • Elena Kuznetsova: I have killed an hour to figure out how to sync Google contacts' birthday and I have this done! U [...]
  • Andy: I have been trying to figure this out forever.....Thank you ever so much!! [...]


This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States.