Using a file as a swap memory (SWAP)

Author Maikel Llamaret Heredia Posted on the site of GUTL.

Long ago, GNU / Linux, was limited to using a single swap memory partition of a maximum of 128 MB, something highly criticized by critics of the Linus Torvalds core.

Fortunately, today there is no such limit, and it is also possible to use as much swap memory as required to satisfy the needs of any system.

Sometimes, after installing our system we find ourselves in the need to increase the memory SWAP that we had configured during the installation, something that can be done in a simple way by increasing the size of the partition chosen for these purposes. But ... What to do if you don't want to touch the partition table?

What is SWAP and how does it work?

The swap memory space or swap, is what is known as virtual memory. The difference between real and virtual memory is that virtual memory uses hard disk space instead of a memory module.

When real memory is exhausted, the system copies part of its contents directly to this swap memory space in order to perform other tasks.

Using SWAP has the advantage of providing the additional memory needed when real memory has run out and a process needs to be done. The disadvantage is that as a result of using space on the hard disk, the use of the hard disk is slower.

Use a file as a swap memory.

This method does not require making changes to the hard disk partition table. Ideal for inexperienced users, for those who want to avoid taking risks when changing the partition table of their hard disk, or for those who require more than occasional swap memory, or in a circumstantial way.

Considering that the swap file can be placed in any directory on the hard disk, the command is executed dd, specifying that zeros will be written (if = / dev / zero) to create the file / swap (of = / swap), in blocks of 1024 bytes (bs = 1024) until completing a certain quantity in bytes (count = [quantity multiplied by the value of bs]). The following example does the above until 524288000 bytes are completed (divided by 1024 equals 512MB):

To create the file that we will use as SWAP, we will open a console and type the following (as root):

dd if=/dev/zero of=/swap bs=1024 count=512000

The above step may take a few seconds, please be patient. Then, to format the created file as swap memory, we will execute the command mkswap, as follows (always as root):

mkswap /swap

You will be returned an output in the console similar to the following:

Setting up swapspace version 1, size = 511996 KiB no label, UUID = fed2aba5-77c6-4780-9a78-4ae5e19c506b

To activate the partition, and have it used immediately by the operating system, execute the command swapon. In our case we will activate as a swap memory partition the / swap file that we created and formatted SWAP in the previous steps:

swapon /swap

To verify that the new swap file is being used by the operating system, we will execute the command again free and we will see that the capacity of the new file has been added to the initial SWAP memory.

In order for this file to be used as swap memory automatically at the next system boot, we will edit  / etc / fstab (using nano, gedit, kate, kwrite, vim or the plain text editor of your choice), adding the corresponding line, as follows, where instead of the device, the path of the swap file created is put:

We open the file

nano /etc/fstab

And we add:

/swap         swap      swap     defaults               0 0

Ready!!!!

We can reboot the system and test that our swap memory has increased with the use of the new file SWAP. Simply what we have done is create a file in the root of the system, format it SWAP and tell our GNU / Linux to use it as a swap memory, together with the partition that we already had destined for this purpose.

Something simple but that could be of some use to many of us ... Without further ado ...


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

25 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   khourt said

    The note is great. Only now it makes me think one thing, could I similarly use a USB as a memory stick ??? If you have a somewhat old equipment and getting the memories is complicated or too expensive (it seems that the older the more expensive the spare parts in computers), it would be good to be able to expand the memory only with a USB

    1.    auroszx said

      Yes, it is very good, more or less that is how the zramswap that I have installed does (I don't like partition salads, I don't separate anything).
      If you want to use the usb as a swap, you just format a partition for that purpose, and add it to the fstab, changing / swap to / dev / sdb1 (assuming the path of your usb is sdb1).

      1.    sieg84 said

        you will be happy with btrfs and its subvolumes

      2.    khourt said

        Well, if I understood correctly how to do the USB Swap, but I thought more about something like additional RAM, since the USB would be physical memory, and I would not like to have to wait for it to exceed 60% of the memory to start to use USB.

        Well and seeing the comment of @nonamed, why solutions like this (and only as a contribution and not as the start of a team war and with all due respect) at a time where there are already teams with better performance and it seems unnecessary; I can think of 3 points:

        1st. And the most obvious, to use it in old equipment
        2nd. I am also thinking of not so new laptops that have a capacity of 1gb or 2gb, "MINUS" the memory destined for the video card
        3rd. Well, it's obvious, right? Because it is interesting and because it can be done ... hehehe! XD

        a greeting

        1.    k1000 said

          Or if you forget to create the SWAP and you don't want to mess with the partition table

        2.    Katekyo said

          Do you decide to use a USB drive as RAM? That cannot be done because the RAM's read-write rate is much faster than a USB can support and also the USB would be destroyed after a short time of use haha ​​and it would be good use a USB drive as an additional SWAP

    2.    Altair said

      Yes, you create a swap partition on a usb and mount it with sudo swapon / dev / sdX and you can add -s 60 to it so that it has priority over other swap partitions. You can also add it to the fstab so that it is mounted automatically, or create the swap file as it says in this great guide on the usb 😉

  2.   slayerkorn said

    Excellent guide, and I practice at the same time, for when one runs out of swap, although difficult. Now I am replicating the same thing but in a usb memory to make the transfer of data from physical memory to swap memory a little faster.

  3.   elendilnarsil said

    Thanks for the info. but it generates a question, maybe something silly: once this swap file is created, can't the swap partition be deleted ???

  4.   Nonamed said

    Sometimes I wonder if it is really worth having swap, I always have it at 0%, maybe it has to do with the memory, I have 4Gb of ram, I guess the more memory it makes less sense to use swap

    1.    Daniel Rojas said

      Yes, it depends on the memory you have in the machine. I also always have it at 0%, and having 4Gb of ram I only give 512mb to the swap partition

      1.    pzero said

        In the last installation I have removed the swap. Still, with 24GB of RAM, I have seen small swap uses (a few KB) on occasion even with much of the RAM unused. Why? I have no idea

  5.   sieg84 said

    I had to do this in sabayon, I could not find the cause of why it ate all the RAM and the swap.

  6.   mr linux said

    I will always say it, DesdeLinix is ​​the number one blog, thanks to these interesting articles. By the way, I found the key that Yoyo threw from the jail where they have Elav, I think I'll keep it.

    Greetings Elav.

  7.   merlin the debianite said

    Very useful thanks for the contribution.

  8.   proper said

    It is easier
    lvm lvresize /dev/vg_laptpop/vl_swap -L +4G
    if you are using LVM clear (assuming you want to swap 4gigas)

    1.    proper said

      However, that contribution is p% $ to mother, not having known it before, how many formats would I have saved xDDDD

  9.   invisible15 said

    I have two swap partitions (one on each hard drive) and with 1GB of ram, I use a minimum of between 200mb and 500mb of swap ...

  10.   kalh said

    If you have several old USB pen-drives you can swap on a raid 0 strip of N USB devices, thereby speeding up the swap performance in proportion to the punctured USB devices and whenever possible each one in a different root hub -I think ! -. You just have to inform yourself about the native support of raid by linux soft.
    a link for more info:
    http://www.kriptopolis.com/raid-1
    or a more general search
    https://www.google.es/search?q=raid+por+soft+en+linux&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb

    1.    kalh said

      I forgot to say that raid can be done at partition level. So if the usb spikes are not the same size, you can make partitions the size of the smallest spike and the remaining holes of the larger usbs can be used for other things or for more swaps at the file or partition level ... which is not Is it possible to do raid with several interchange files with the method you describe ???…

  11.   Carlos said

    Excellent tutorial, I was urged to increase the swap that I had available. (AND)

  12.   George said

    This looks good. I will try with a usb memory.

  13.   TheGuillox said

    excellent… does it work on an android cell phone? I already have a custom kernel, but I don't want to partition the sd. I guess this would have to work

  14.   delugas said

    Very good article.

    Another interesting concept about Linux swap is swappiness:

    http://www.sysadmit.com/2016/10/linux-swap-y-swappiness.html

  15.   David coleman said

    Hello my name is David Coleman I'm 32, from Ohio I'm studying Computer programming && computer science to get my Associates degree in both within 4years time !,
    My question with application (SWAP / no-root) APK is simple after the swap / swp file I created for 2Gb 999Mb's × 2 essentially how an exactly where in my Android is it to be stored? I'm using a 3Gb ram 32gb memeroy +32 gb SanDisk SD too boot Stylo 5 by boost mobile Unrooted device with bootloader unlocked as well as sim unlocked !. Any advice is well appreciated an welcomed thx
    Urs SENCERLY, David Coleman 32 from Ohio UsA .. 🌷