If you have already made a swap file during setup of ubuntu, would the commands mkswap and swapon add to it or it replaces it? Is there a way to add to it rather than replace it? Is there a way to do it while you are not sudoer?
Asked
Active
Viewed 2,051 times
1
-
In parallel to your question, see this answer and consider removing SWAP altogether – Robert Riedl Jul 11 '18 at 11:46
-
@RobertRiedl This is bad advice. Swap has advantages even when you have a lot of RAM. See https://chrisdown.name/2018/01/02/in-defence-of-swap.html for a detailed discussion. TL;DR: Swap won't hurt if you do not need it (and may be used to make place for better caching), but there are times when you will need it and having it avoids a lot of trouble. – allo Jul 11 '18 at 12:07
-
@allo, I said consider removing. No amount of swap will ultimately protect you from the OOM and if you have swap, linux will use it and it will be slower than without swap. If you don't have the diskspace to spare or if your disks are old and slow, I would advise against. Interestingly we just discussed the merits of swap in server and VM environments. But this is not the place to discuss the pro and cons of swap, I just wanted to point out to OP that you might consider doing away with swap. – Robert Riedl Jul 11 '18 at 12:21
-
Yes and you should not consider removing it, as swap is an elementary feature of linux memory management and the kernel does a good job managing it so it does not slow down your pc. Please read the accepted answer about tuning it in the question you linked. – allo Jul 11 '18 at 13:43
-
1Thanks guys for the valuable input. I asked this question because I am running metagenome sequence assembly on the department's server using SPAdes which is a memory greedy algorithm. When it reaches the end of the RAM, it crashes wherever it is in the assembly, sometimes so close to the end. So, I needed to extend the RAM by some extra gigabytes for it to finish. Server admin says that there is already a swap file. Can I extend on it then removing this extra? – Mohamed Malash Jul 11 '18 at 14:10
-
I suggest that you keep that extra RAM. Swapping while assembling will probably make things very slow. Use the swap only for an extra margin to avoid crashing due to lack of memory. – sudodus Jul 11 '18 at 14:28
-
SWAP is never a replacement for RAM. You have to make sure there is enough RAM (+ overhead) for your software to run. SWAP is only there to store memory pages that are infrequently used - which won't be the case in your assembly - and as an emergency space if RAM runs out. But as already mentioned, swapping will make your software really slow and in your case might not even prevent crashing. – Robert Riedl Jul 12 '18 at 07:32
-
@RobertRiedl do you mean it may not be used as backup for the software to keep running and may crash as if there is no extra swap? have you tried it? – Mohamed Malash Jul 12 '18 at 15:35
2 Answers
4
You can
create a partition and make it a swap partition with
mkswap
orgparted
add a corresponding line in the file
/etc/fstab
to make it active at boot. You can usesudo swapon -a
to make all swap devices (files and partitions in
/etc/fstab
) active.You can also create a second swapfile (with another name), or maybe better, replace the current swapfile with a new one.
But first you should consider, if you really need more swap and in that case why and how much.
Managing swap is a typical task, where you need sudo
permissions.

sudodus
- 46,324
- 5
- 88
- 152
-
Good point regarding the second swapfile. Sadly, for the last statement. Thanks dear – Mohamed Malash Jul 11 '18 at 14:16
1
The command swapon <specialfile>
adds a swap area prepared with mkswap <device>
.
swapon --show
displays the active swap areas.

muclux
- 5,154
-
-
Yes, each
swapon
adds a swap area, as you can verify withswapn --show
. – muclux Jul 11 '18 at 18:27