Create a swapfile for your Linux server

(Kane York) #31

The default filesystem is not xfs, so no, that problem will not show up by default.

I guess add instructions to fallback to dd if you get the error message swapfile has holes?

(Dmitry Krasnoperov) #32

I’ve just moved to higher plan.
How to remove swapfile?

(Jeff Atwood) #33

You can leave it, the swappiness is set to a very low level so it will not be used except in case of emergency.

(Fábio Machado De Oliveira) #34

Having at least some swap can increase performance. It will store data that isn’t being used and allow more space for disk caching. See the “Why do I need Swap?” section from Ubuntu’s Swapfaq

(Fábio Machado De Oliveira) #35

I’d like to suggest replacing the command:

    dd if=/dev/zero of=/swapfile bs=1k count=2048k

with this:

    fallocate -l 2G swapfile

Because it creates a 2G file instantly, without initializing it’s data.

(Jeff Atwood) #36

This was covered upstream, please scroll up. May cause problems later if the swapfile is fragmented.

(Daniel) #37

Note that swapfiles do not properly work on btrfs filesystems.

(Sam Saffron) #38

We are currently recommending against btrfs, have not seen any perf improvement using it and have seen all sorts of strange issues.

If you are going for cutting edge I would try overlay

(ljpp) #39

I came back to this zRam/zSwap idea and decided to give it a longer test run on my low traffic site hosted on a 1GB Digital Ocean slice. Actually the idea came back and Microsoft just enabled RAM compression for all users on Windows 10 and it seems to be doing its job on my cheap Baytrail tablet.

Anyway, been running for 24 hours now and so far the system has not yet touched the physical swap at all. I also radjusted the Swappiness back to default value 60, rather than 10.

FatMoomin:~# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/swapfile                               file            2097148 0       -1
/dev/zram0                              partition       508936  208060  5

So far nothing unusual in the CPU loads either, but again this is a low traffic site. Few hundred views per day and most are passive readers.


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 ?

Link dialog New Topic does not always include context
(Jay Pfaffman) #41

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.


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 !

(Dylan Hunt) #43

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

(Michael Downey) #44

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

(Jay Pfaffman) #45

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

(Jeff Atwood) #46

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

(Bruno) #47

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?

(Jay Pfaffman) #48

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.

Can swap file cause delays?
(Bruno) #49

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

Thanks for the info!

(Benjamin lupton) #50

Ran into Errno::ENOMEM: Cannot allocate memory and Cannot allocate memory during upgrade 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