Create Swap Memory in Linux
Linux - Ubuntu Terminal

Create Swap Memory in Linux

You've just installed Linux on your machine. To make even things better, you are dual booting between Windows and Linux.

Windows is the most popular OS in the world. If you're like me, you have a couple of games installed there. (I still got my eye on you NFS: Most Wanted :) ). Dual booting also helps when you have software exclusive to Windows like Microsoft applications.

Why is Linux slower than Windows on the same machine? Well, if you are running a lot of apps on your kernel, installed node packages (yes, this is a thing), and running servers/docker containers, your RAM may be constrained.

Swap memory is memory located on the hard disk that has been set aside for RAM. It helps when there is an overflow in app memory. Though retrieving app data from Swap is slower than RAM, it is necessary as a safeguard in extreme situations. (Think running a while loop in C with no breaks. :D ).

To create swap memory in Linux follow the following steps.

  1. Check if Swap memory has been allocated in your machine

sudo swapon --show        

If the output is blank, then your machine has not been allocated Swap memory.

free command in linux
free -h command

The free -h command helps us check machine memory and allocation given to Swap. (In the image above Swap has been allocated. For your machine, this should be 0B.

2. Check disk usage

Current disk usage is checked by;


df -h        
No alt text provided for this image
df -h

The / partition is the one with disk space. On my machine, I have allocated around 200 GB for Ubuntu, with filled capacity at 12%. In most cases, 4GB of Swap Memory is enough. Check out David's Article on exact space to allocate.

3. Creating Swap Memory

Swap will be allocated on the root directory. This done using the following command.


sudo fallocate -l 4G /swapfile        

In the example above, we are allocating 4GB to swap. We confirm this by checking if the file has been created.


ls -lh /swapfile        
No alt text provided for this image
The result from ls -lh

Change the permissions to be accessed by the owner/creator of file; in our case it's root.


sudo chmod 600 /swapfile        

4. Enable the Swap Memory

Mark the file as swap by inputting the command below.


sudo mkswap /swapfile
        

The output will be something similar to this. Additional text will be dependent on Swap Memory allocated.



Setting up swapspace version ...        

Enable swap by;


sudo swapon /swapfile        

Then check if Swap has been created by,

No alt text provided for this image
sudo swapon --show

Run free -h to check memory allocation.

No alt text provided for this image
free -h

5. Make the Swap File Permanent.

When you trigger a reboot on your linux by the commands below from terminal, or manual reboot, the changes above will not be saved.

System reboot using the terminal
System reboot in the terminal

Thus, you will need to add Swap file to /etc/fstab. NB: Be careful during this step as any misconfiguration can damage your Linux OS.

No alt text provided for this image
sudo nano /etc/fstab

Add /swapfile none swap sw 0 0 at the end. Then press Ctrl + O, and Ctrl + X to save and exit.

Edit /etc/fstab
Edit /etc/fstab

Then reboot machine.

6. Swapiness Property

What do we mean by this? As described at the start, reading app data from Swap is slower than from RAM. The swapiness parameter is a reading which shows amount in percentage the frequency to which the kernel will spill over to Swap from RAM.

For better performance especially in servers, we want this to be as low as possible. This will make sure the kernel will swap data to swap when necessary.

Values closer to 100, indicate the kernel will put more app data to Swap in an attempt to free RAM. As the user, it is important to know machine usage and apps which you will be running.

To configure this, let's first see which value has been allocated by default.


cat /proc/sys/vm/swappiness        
No alt text provided for this image
Check Swapiness Property

On my machine above, I have set the value to 10. To do this write the following command.


sudo sysctl vm.swapinness=10        
No alt text provided for this image
Set Swapinness Property

To make sure the value doesn't change during reboot, add the command at the bottom of /etc/sysctl.conf file.


sudo nano /etc/sysctl.conf        
No alt text provided for this image
Edit /etc/sysctl.conf to save swappiness

Add the command at the bottom, then Ctrl + O, and Ctrl + X to save and exit.

No alt text provided for this image
Add Swappiness value to save

Conclusion

Swap memory is dependent on machine architecture too. A PC with at least a higher RAM, better and more recent processor, and preferably an SSD will always perform better than a machine of lower specs.

However, if you are experiencing financial constraints when trying to upgrade your machine, you can follow the steps above.

Happy Linux. (Yes, this is a greeting among us Linux users. :) )

要查看或添加评论,请登录

Lee Nduati Francis的更多文章

  • Windows 11 Cannot Connect to Windows 10 through Network Sharing.

    Windows 11 Cannot Connect to Windows 10 through Network Sharing.

    Hello IT friends. I'm back again with another printer article.

  • Printer Errors

    Printer Errors

    Hello IT Support Techs, we have all come across deadly pop-ups when connecting shared printers. I know I have! And I…

    1 条评论
  • ChatGPT: Unleashing the Power of AI Conversations

    ChatGPT: Unleashing the Power of AI Conversations

    Introduction: In artificial intelligence, ChatGPT has emerged as a transformative force, reshaping how humans interact…

  • What is RAM?

    What is RAM?

    We have all heard about RAM. I am looking to upgrade my computer; hence I will buy a machine with more RAM.

社区洞察

其他会员也浏览了