How to Create a Swap File in Linux


Swap within Linux are specific areas on the disk that are reserved as virtual memory. They are primarily used to enhance system performance when dealing with resource heavy tasks such as video editing. When the system starts to struggle, the kernel will move inactive processes into swap to make room for active processes within working memory.

Ordinarily, within the Linux installation, a swap partition will be created for you by default and will allocate space on the hard disk for this purpose. This has a number of drawbacks, such as space if you have a a smaller disk on an older computer, or if you are using an SSD on a newer device.

The issue with SSD drives is that they have limited write capacity within the cells. Even with wear levelling, flash memory has a finite lifespan, and multiple writes can render the individual cells unusable.

What’s the alternative?

If using a dedicated swap partition is not practical, or you simply want to try an alternative and not spend money on extra RAM, then you can use a swap file instead.

A swap file functions in a similar way to a partition, although it had the added benefit of users being able to control the size without the issue of resizing a volume. In addition, how dedicated the swap will be utilized, or the “swappiness” factor, can also be controlled by modifying the swap value.

Swap Creation

I will run through a basic example of creating a 1GB swap file.

First create the file by entering the following command within your Terminal:

If you don’t have fallocate installed, then run the more traditional command:

Now format the swap file:

Add the swap to the system as a swap file:

Open the “/etc/fstab” within your favourite text editor, and add this to the end to make the change permanent:

The line above breaks down as follows:

  • “/mnt/1GB.swap” – this is the device and file name
  • “swap” – this defines the mount point
  • “swap sw” – this shows the swap file will be activated by swapon – s (see below)
  • “0 0” – these are the options used by the dump program and the fsck command respectively

At this point, if you want to alter the “swappiness” value, then you can by editing “/etc/sysctl.conf” in the same manner as you edited the fstab above. The swappiness value is typically 60; the higher the number (up to 100) the more aggressive the swap.

The amount on swap needed depends on how the system performs and how memory is being used. Users should experiement to find what is best for them. If the value above is set to zero, then the swap file will only be used when the system has exhausted the memory. Values above zero will let the system swap out idle processes and free memory for disk caching; this can potentially improve overall system performance.

Finally, check if the swap is active:

Simply reboot and you will have a working swap file as opposed to a swap partition. Which option is best for you? Do you use a partition or use a dedicated file? Let us know in the comments and and also tell us any alternate methods you may have for generating a file.


  1. What is the Golden Ration between the size of RAM and the size of a swap file?

    1. Typically, it’s a 1:1 ratio…1 GB of swap for every GB of RAM. I’ve seen some places suggest twice as much swap as RAM and if the space is available, that can’t hurt. But if you think about it, in the absolute worst case scenario possible, the most the computer will want to write to swap is the entire contents of the RAM, so having the same amount of swap as RAM is all that’s really needed.

  2. Question:

    Sometimes I read, that swap is not necessary when there is enough ram in the maschine.
    what is enough ram?


    1. “what is enough ram?”

      Depends on what you’re doing. If you’re doing video editing, I’m not sure it’s possible to install enough RAM to prevent swapping…I hear video editors suck up RAM like it’s going out of style :-) But for typical uses (web browsing, e-mail, social media, watching videos and the like), a machine with 16 GB probably isn’t going to swap any, and you might even be able to get away with only 8 GB. Programmers tend to like 32 GB or more…compilers are also notorious for sucking up RAM, and when you’re compiling a program, the less the machine writes to swap, the faster the program compiles. And believe me…compile time is a **big** deal :-)


      Greetings, entity from another place…hope you’re enjoying your visit. And if you are, don’t come to Edmonton, Alberta, Canada…unless, of course, you actually **like** snow :-(

  3. @dragonmouth:
    The ratio depends on the amount of RAM, in the old days when you had little RAM it was set to 1.6*RAM = SWAP
    When you have a lot of RAM and you only need SWAP only for emergency when you for a short while may run out of RAM you may only need 0.2*RAM = SWAP. If you want to hibernate a laptop then you may want to have at least 1.2*RAM = SWAP
    So it’s much up to what you want to do with the computer and with how small SWAP you feel safe with.

    It’s the amount where when you do use your machine with extreme memory usage without anything swapped out into the SWAP, then you have enough. There ain’t a number on it as hos you use your computer ain’t the same way as I use mine.

    @Matthew Muller:
    You forgot to mention that SWAP-file is slower than SWAP-partition as the first one has the overhead of the file system and you need to be able to access it before you can wake up your hibernate machine.

  4. Kazaam has posed the question, “what is enough ram?”

    Enough RAM is enough RAM for all the memory demands of the processes running at any one time so that the process memory does not have to be “swapped” out of RAM to disk to allow something else to be run.

    Your question is almost similar to “how long does a piece of string (or rope) need to be?”

    To which the answer is “long enough for the purpose which you intend to use it”.

    If you only live on the 2nd floor of a building, your rope only has to be long enought to reach the ground from the window, but if you intend to do mountain climbing it must be much longer.

    Similarly if you are just running some mundane tasks such as web browsing and e-mail, then 4 GByte of RAM is probably enough, but if you intend on doing extensive video editing or running some large databases or running lots of Monte Carlo calculations, you will need significantly more.

  5. Both are good questions. I’d like to know too.

  6. “This has a number of drawbacks, such as space if you have a a smaller disk on an older computer, or if you are using an SSD on a newer device”

    It should be noted that in either of these circumstances, a swap file is as much a “problem” as a swap partition. The swap file still takes up space on the hard drive, just like the separate partition did. And with an SSD, it doesn’t matter if it’s a file or a partition, data is still being written to the device, slowly wearing out the cells…meaning that the only real solution is to put the swap partition/file on a separate hard drive. Which is actually a good way to make use of older, smaller hard drives :-)

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories