DEV Community

loading...
Cover image for How to Make a Swap Space Using a Swap File in Linux

How to Make a Swap Space Using a Swap File in Linux

CrazyOptimist
https://crazyoptimist.net/about
・2 min read

This article has been ported from my personal blog
You might have been stuck in memory leak when building some kinda javascript-heavy frontend apps like Vue or React on a cloud server itself. Just for serving built artifacts, we do not need large instances, but still the building process is always hungry for more memory you know.
Below is how to resolve such specific problem.
Oh, this is not the case if you already have a dedicated(partitioned) swap space, it's a better option in any case but in most cases, we will be playing with servers without a swap patition.
Okay, let's get started.
As a general rule, calculate swap space according to the following:

Amount of physical RAM Recommended swap space
2 GB or less 2 * Amount of physical RAM
More than 2 GB, less than 32 GB 4 GB + (Amount of physical RAM – 2 GB)
32 GB or more 1 * Amount of physical RAM

Important Note: Swap sapce should never be less than 32 MB!

Steps

Create a swap file

sudo dd if=/dev/zero of=/swapfile bs=128M count=YOUR_COUNT
Enter fullscreen mode Exit fullscreen mode

In the above command, bs is block size which should be less than the available memory at that moment.
bs * YOUR_COUNT will be your swap file size, for example, bs=128M count=32 means your swap file will be 4096 MB in size.

Update the read and write permissions for the swap file

sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode

Set up a Linux swap area

sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

Make the swap file available for immediate use by adding the swap file to swap space

sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode

Verify that the procedure was successful:

sudo swapon -s
Enter fullscreen mode Exit fullscreen mode

Enable the swap file at boot time by editing the /etc/fstab file

sudo vim /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Add the following line at the end of the file:

/swapfile swap swap defaults 0 0
Enter fullscreen mode Exit fullscreen mode

That's it.
Happy coding!

Discussion (1)

Collapse
louislow profile image
Louis Low

I install preload to dynamically adjust swap space without hassle.