Go to TogaWare.com Home Page. GNU/Linux Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Swap

If you run many applications at once, each of them is using RAM. Thus it is not difficult to fill up RAM, in which case your computer would probably stop being able to do anything. Linux uses the idea of swap memory to extend RAM with disk space, so that even if all your applications are using more memory than available in RAM you can still continue working! Swap partitions and swap files can be considered as extra (but slower) RAM! The real solution for improving such situations is to install more RAM.

If you are running out of swap memory you can add more by creating a swap file on disk. Swapping to a file is a little less efficient than swapping to a dedicated swap partition as the kernel needs to access the new swap file through the file system. But since this swap file is only called upon when you've run out of RAM and then swap memory on a dedicated partition, the additional performance hit is not much compared to that of swapping itself.

Have a look at what memory you have available:

  # free
               total       used       free     shared    buffers     cached
  Mem:        256216     252208       4008     118100      56996      46944
  -/+ buffers/cache:     148268     107948
  Swap:       224900      51268     173632
Here we have 256MB memory and 225MB of swap of which 51MB is being used. Or else use the swapon command to find out about just the swap memory:
  # swapon -s
  Filename   Type       Size    Used    Priority
  /dev/hda5  partition  498920  258728  -1
Here we have almost 500MB of swap of which 258MB is being used.

Next create a file without holes and a multiple of 4K in size (in this example, adding about 256MB):

  # dd if=/dev/zero of=/extra-swap bs=1024 count=262144
  262144+0 records in
  262144+0 records out
Or perhaps add a 512MB file:
  # dd if=/dev/zero of=/extra-swap bs=1M count=512
The input file (if) /dev/zero is a device file that will give us zeroes to be written to the output file.

Now mark the new swap file as a swap file by writing a signature to its beginning which contains some administrative information used by the kernel:

  # mkswap /extra-swap 
  Setting up swapspace version 1, size = 268431360 bytes

Tell the kernel about the new swap file:

  # swapon /extra-swap
And check that it is there!
  # free
               total       used       free     shared    buffers     cached
  Mem:        256216     252448       3768     118100      56996      46944
  -/+ buffers/cache:     148508     107708
  Swap:       487036      51268     435768

To remove the swap file from usage:

  # swapoff /extra-swap

You can add to /etc/fstab:

  /extra-swap        none        swap        sw     0     0
Then on boot when swapon -a is run the new swap will be added to swap space.


Support further development by purchasing the PDF version of the book.
Other online resources include the Data Science Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2020 Togaware Pty Ltd. Creative Commons ShareAlike V4.