I want to add a swapfile of 2GB to my system. I have found this code for that

sudo fallocate -l 2g /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

As I have never used swapfile or swapspace. I want to know at what location a swapfile is created. Will the file be created in the home directory? What is the best location to create a swapfile? Home directory, root directory or other mounted disks?


In your case the swapfile is "/swapfile" – but you could change that to your liking, just replace "/swapfile" with your desired destination.

Best location is of cource opinion-based, but I would prefer fast storage (i.e. SSD - but keep in mind it has limited life span, so that is highly opinion-based), if the system swaps a lot.

If you'd like to use that sawp file every time you boot, you can add an entry to your /etc/fstab like

/swapfile    none    swap    sw      0 0

You might consider using a drive wich isn't under constant high IO-load because of something else.

|improve this answer|||||
  • 1
    You don't want swap on an SSD if possible, because an SSD's life is determined by the number of writes to it. Thus, it is preferable to have swap on a HDD. – ravery Mar 20 '18 at 3:16
  • Because the number of writes until a SSD is done is quite high, I'd still prefer to use SSD over HDD. Of cource depends on how much the system swaps. Longterm I'd consider adding so much RAM that swap isn't needed at all. – kai-dj Mar 20 '18 at 10:09
  • Actually we can tell the system how to behave when a swap is in use. For example sudo sysctl vm.swappiness=10 and sudo sysctl vm.vfs_cache_pressure=50 will cause very rare swap usage - it will be used as a last resort. You can place these commands to the bottom of /etc/sysctl.conf to make the changes permanent. More information is provided here: digitalocean.com/community/tutorials/… – pa4080 Mar 20 '18 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.