Create a swapfile for your Linux server

Hi !

I have a question : I created a 1 GB swapfile, it is activated and works for months now but it’s recommanded to use a 2 GB swap for my config, what will be the command to increase it to a 2 GB swapfile ?

4 Likes

swapoff /swapfile and follow the directions to make a 2GB swapfile.

You don’t need to re-do the file system table, swappiness and so on.

8 Likes

Thanks It worked

If it helps others, I’ll give a little extra : I had to stop the app because the server couldn’t allocate the memory. So I run these commands :

cd /var/discourse
./launcher stop app

I run the command @pfaffman gave me, did a new swap, started the app and rebooted my server

cd /var/discourse
./launcher start app

Then for the reboot : shutdown -r now (I don’t know if it was neccessary, but it never hurts)

And voilà

Thanks again !

9 Likes

but if the swappiness is 10, isn’t it only used in emergencies?

1 Like

0 is “unless absolutely necessary”, 100 is “instantly”.

6 Likes

Yes, and that’s the point. The only time swap is needed is during upgrades.

1 Like

6 posts were split to a new topic: Can swap file cause delays?

After running Jeff’s command to create the swap file, do I need to shutdown my server or something to make it work?

I did a git pull and rebuild app just to make sure it was all good. Not sure if it was necessary anyway.

Do I need to execute a shutdown as well? What would be the commands for that?

Also, is there any command I can run to make sure the swap file is working properly?

1 Like

The swapon command turns on the swap file (Jeff’s notes say “activate”). You don’t need to reboot (you could use sudo reboot to reboot if you wanted to for some other reason).

free will give you memory info, including swap.

4 Likes

Oh I see. Great. No need to reboot then. Swap seems fine then:

Thanks for the info!

2 Likes

Ran into Errno::ENOMEM: Cannot allocate memory and https://meta.discourse.org/t/cannot-allocate-memory-during-upgrade/27619?u=balupton so tried this, but got:

root@bevry-discourse-ubuntu-1gb-sgp1:~# sudo install -o root -g root -m 0600 /dev/null /swapfile
install: cannot remove ‘/swapfile’: Operation not permitted
1 Like

Do you have swap on already?

free will show you swap.

1 Like

Anyone have any advice on if I need a swapfile if my server has 4GB of memory? If so, what should the size be?

1 Like

You are not required to. If you wanted to play it safe, a 1 GB file should be more than sufficient and rarely used.

8 Likes

Hello ,

There is any sense to create 3 GB , 4GB SWAP for instance with 1 GB RAM . Any profits here ?(1% more better work is a profit )

You are better off investing in more physical RAM than a larger SWAP file.

4 Likes

Obviously you can create a swapfile any size you want. If it is not needed, it will just sit there merrily holding zeros. The disk space will simply be wasted.

So the trick is to size your swap large enough for the heaviest operation, which in Discourse should be the rebuild. Check memory usage on a rebuild to see how much is needed.

I think there are studies out there saying that very large swap files will not be useful because the CPU is gonna start thrashing way before that much swap is used.

EDIT: Searching on the net, it is recommended the swap to be no larger than 2 times physical RAM due to declining returns of adding more.

./discourse-setup will now create the swap file, so there is (usually) no reason to do it yourself. If you have a too-small swapfile you should delete it before running discourse-setup.

5 Likes

This is useful as a quickly grab-able recipe for adding swap files, but the use of sudo in the copy-paste script is inconsistent. Currently it only works if pasted in as root, and the sudo on some commands does nothing in that case. The use of sudo is a nice addition, but only if it’s done throughout.

This comment mostly won’t get read, so maybe the original post could be edited?

2 Likes

Sure I’ll remove it and put a notice at the top