Create a swapfile for your Linux server

Would enabling zram be a good option (ubuntu: sudo apt-get install zram-config)? People seem to claim that it is able to provide ~1:3 compression ratio. On an 1GB Ubuntu box, the zram-config installs a 512mb zram compressed virtual swap. Elementary school math says: 0.5GB * 3 + 0.5GB = 2GB.

1 Like

Possibly could help on extremely low end RAM, I don’t know, you would have to run benches.

see also

1 Like

I’ve read a ton of articles on zRam and do know how it works. The articles or benchmarks usually focus on desktop usage.

On my 1GB box and a low traffic website the Discourse Docker installation is already causing a little bit of swapping, so enabling zRam might be beneficial. However, zRam is basically trading CPU cycles for RAM, which is something I am not overly excited about on a single-core VPS.


I was just about to say this – if you are low on RAM but have lots of CPU (fast dual or quad core) than it might make sense. But if you have both low RAM and not much CPU power this is unlikely to improve things for you.


Not to mention that your system will have a meager 512 MB of active RAM left to work with and common wisdom (also: sciency resource allocation models) holds that cutting the available RAM in half increses required swap operations by a LOT more then factor two.

Didn’t we learn that lesson back in the days of the memory-doubling placebo-software for Windows 3.11? :grin:

1 Like

I moved 7 posts to a new topic: Why does Docker say “WARNING: No swap limit support!”

May I suggest:

fallocate -l 1G /swapfile (or 2G)

Simply reserves as uninitialised, rather than actually writing out 1(2)G worth of zeros. Returns a tad quicker.


It returns quicker as it doesn’t right out all the zeros, true. But this might lead to problems on some filesystems if fallocate doesn’t actually allocate the space as it claims - you’ll up with a sparse file which doesn’t work for swap.

It should work fine, just be aware that if swapon complains you’ll need to dd the file to remove the holes.


I tried it on a new DO install and it did not seem to complain and free -m showed the expected swap. So… working? At least for default Ubuntu 14.04 x64 LTS install?

1 Like

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?


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

1 Like

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


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

1 Like

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.

1 Like

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


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 ?


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 !


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