How to create a Swapfile - which can be a temporary thing if you so choose

Here is where you can post tips and tricks to share with other users of MX. Do not ask for help in this Forum.
Post Reply
Message
Author
User avatar
handy
Posts: 611
Joined: Mon Apr 23, 2018 2:00 pm

How to create a Swapfile - which can be a temporary thing if you so choose

#1 Post by handy »

Note: Let me know if you have any problems with this?

Introduction

The swapfile will add swap memory on top of whatever swap partition you may have. If you don't have a swap partition, then you can easily use a swapfile instead. It can easily be turned on/off separately '''OR''' together with an existing swap partition - treating all swap as one.

Creating a /swapfile

This is a really quick & simple process (unsupported by BTRFS).

Following is how we create a /swapfile:

Set the size

Use M for megabytes & G for gigabytes, the following creates a 4GB /swapfile.

Code: Select all

 # fallocate -l 4G /swapfile
Set the correct permissions (very important for security)

Code: Select all

 # chmod 600 /swapfile
Format the /swapfile

Code: Select all

 # mkswap /swapfile
Activate the /swapfile

Code: Select all

 # swapon /swapfile
Add the following line to your /etc/fstab

Code: Select all

 /swapfile none swap defaults 0 0 

Removing the /swapfile

The swapfile if very easily removed.

Turn it off

Code: Select all

 # swapoff /swapfile
You can also simply turn off all swap; both (swapfile and swap-partition) with this:

Code: Select all

 
 # swapoff -a
Remove it

Code: Select all

 # rm -rf /swapfile

Edit your /etc/fstab

The line that calls the '''/swapfile''' needs to be either removed from '''/etc/fstab''' or commented out by placing a '''#''' at the beginning of its line.

To do this you need to open your favourite text editor with root privileges.

As an example, you could open gedit in the Terminal using the following command:

Code: Select all

 sudo gedit /etc/fstab
Change '''gedit''' for the editor that you use.
1_MSI: MAG B560 TORP', i5, RAM 16GB, GTX 1070 Ti 12GB, M2 238GB + USB, MX-23 Fb to Openbox
2_Lenovo: Ideapad 520S, i5, RAM 8GB, GPU i620, HDD 1TB, MX-21 - Openbox
3_Clevo: P150SM-A, i7, RAM 16GB, nVidia 8600, 2x 1TB HDD & M.2 256 GB, MX-21 - Openbox

User avatar
sdibaja
Posts: 47
Joined: Fri May 25, 2018 9:55 pm

Re: How to create a Swapfile - which can be a temporary thing if you so choose

#2 Post by sdibaja »

Great tutorial!
I will give it a spin soon.

Question: My conky (always visible) never shows swap use, none. Could or would a swap file be to my advantage? Thanks
Peter E.
Baja California, Mexico.

User avatar
Adrian
Developer
Posts: 8248
Joined: Wed Jul 12, 2006 1:42 am

Re: How to create a Swapfile - which can be a temporary thing if you so choose

#3 Post by Adrian »

Could or would a swap file be to my advantage? Thanks
It's good to have a swap defined, for example if you have a runaway program if it runs out of memory it might crash your computer, if you have a swap partition or file you might be able to stop it till it crashes. Also if you use VirtualBox or other virtualization you might not calculate (or know in advance) how much free space you have so it might get to use some swap instead of crashing your computer. Now, a swap file has 1 advantage and 2 major disadvantages, the advantage is that you can easily change the size without repartitioning, the disadvantages are that
1. you cannot hibernate to a swap file (at least that was not possible in the past)
2. it's typically slower than a swap partition.

User avatar
Gordon Cooper
Posts: 965
Joined: Mon Nov 21, 2011 5:50 pm

Re: How to create a Swapfile - which can be a temporary thing if you so choose

#4 Post by Gordon Cooper »

Adrian wrote: " It's good to have a swap defined, for example if you have a runaway program if it runs out of memory it might crash your computer, "

Thanks for the memory Adrian. A few years back I was alpha/beta checking an update on a CAD program,one that used vectors and written in C. It started well, but became slower and slower until finally crashing. Found that the author had been allocating memory to deal with the vector calculations. but that he had forgotten a line of code to clear that memory after use. Result was that memory was all used up, until the next piece of calc stalled, and everything came to a grinding halt.

And speaking about C. While learning Pascal, many years ago, I was told that C was a good programming system but that it was very easy to shoot yourself in the foot. Then, along came C++, where it was much more difficult to shoot the foot, but if you did, you shot your whole B**** leg off
Backup: Dell9010, MX-19_B2, Win7, 120 SSD, WD 232GIB HD, 4GB RAM
Primary :Homebrew64 bit Intel duo core 2 GB RAM, 120 GB Kingston SSD, Seagate1TB.
MX-18.2 64bit. Also MX17, Kubuntu14.04 & Puppy 6.3.

Post Reply

Return to “Tips & Tricks by users”