Jump to content

Is SWAP partition needed when using Linux on a modern PC

I want to install Linux Mint on my laptop which has 8G of RAM. I know Swap is sort of virtual memory, and I don't think using my poor 5400rpm HDD as virtual memory can improve performance. Do I need a Swap partition? If so, how much storage should I spend on it? Or is there an alternative similar to the pagefile in Windows rather than a partition? When I last used Linux I didn't really care and just gave 16G for Swap, but I don't have that much storage on my laptop.

Link to comment
Share on other sites

Link to post
Share on other sites

Swap space in Linux is used when the amount of physical memory (RAM) is full.

On default it automatically set the same size as the ram.

Swap partition is mandatory, but you can set it to any size, i would say just try 2 or 4gb.

Ryzen 5700g @ 4.4ghz all cores | Asrock B550M Steel Legend | 3060 | 2x 16gb Micron E 2666 @ 4200mhz cl16 | 500gb WD SN750 | 12 TB HDD | Deepcool Gammax 400 w/ 2 delta 4000rpm push pull | Antec Neo Eco Zen 500w

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, SupaKomputa said:

Swap space in Linux is used when the amount of physical memory (RAM) is full.

On default it automatically set the same size as the ram.

Swap partition is mandatory, but you can set it to any size, i would say just try 2 or 4gb.

Is there a way to have a swap file that has a dynamic size instead of a fixed partition?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, SupaKomputa said:

Swap space in Linux is used when the amount of physical memory (RAM) is full.

On default it automatically set the same size as the ram.

Swap partition is mandatory, but you can set it to any size, i would say just try 2 or 4gb.

you can turn it off how ever read all here and don't just go woot its off bad things can happen. Not devastating just not good. They mention having at least 512 mb is a good idea in case something goes nuts and starts eating up the ram you would end up in kernel panic etc so be warned and read carefully

https://unix.stackexchange.com/questions/224156/how-to-safely-turn-off-swap-permanently-and-reclaim-the-space-on-debian-jessie

Link to comment
Share on other sites

Link to post
Share on other sites

It's already 3 years that i do not use SWAP partition at all. I had ubuntu now for 2 years i have arch linux. 3 years ago i had 6 GB RAM on arch i have 8 GB. I use firefox 15-30 TABs + Pycharm + Steam (i play DoD, CS, L4D 2, Portals). There was not a single problem with my linux machine. I do not need swap at all. My Ram never gets full. :) On fresh start my arch linux installation uses 173 MB RAM :D At max i have seen 5 GB usage.

Computer users fall into two groups:
those that do backups
those that have never had a hard drive fail.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, mate_mate91 said:

It's already 3 years that i do not use SWAP partition at all. I had ubuntu now for 2 years i have arch linux. 3 years ago i had 6 GB RAM on arch i have 8 GB. I use firefox 15-30 TABs + Pycharm + Steam (i play DoD, CS, L4D 2, Portals). There was not a single problem with my linux machine. I do not need swap at all. My Ram never gets full. :) On fresh start my arch linux installation uses 173 MB RAM :D At max i have seen 5 GB usage.

Lucky you never hitting full ram, at work I had to be upgraded from 8 to 16 because my work load was hitting 8gb and use a swap.

 

Tbf I do a network of docker containers that mimic a small part of infrastructure in aws, however you should always have some swap just in case. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/2/2018 at 10:42 AM, SupaKomputa said:

Swap partition is mandatory, but you can set it to any size, i would say just try 2 or 4gb.

No, it's not. Linux is configurable with or without it, you can even remove it after the fact.

System specs:

4790k

GTX 1050

16GB DDR3

Samsung evo SSD

a few HDD's

Link to comment
Share on other sites

Link to post
Share on other sites

On 2018/8/4 at 2:19 AM, vorticalbox said:

Lucky you never hitting full ram, at work I had to be upgraded from 8 to 16 because my work load was hitting 8gb and use a swap.

 

Tbf I do a network of docker containers that mimic a small part of infrastructure in aws, however you should always have some swap just in case. 

I see. So swap is more like an insurance on systems with lots of ram. Is there a way to create a swap file instead of an entire partition?

Link to comment
Share on other sites

Link to post
Share on other sites

If you don't want to make a disk partition - you can use a swap file instead that will grow as needed (much like the windows page file).  Doesn't need to be big - hell even 128MB would be suffient for those odd packages that require swap. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, happymax1212 said:

I see. So swap is more like an insurance on systems with lots of ram. Is there a way to create a swap file instead of an entire partition?

https://askubuntu.com/questions/1031275/increase-swap-in-ubuntu-18-04-under-lvm-and-encrypted-file-system

 

Not your use case but has the steps to create a swap file.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/3/2018 at 10:19 PM, vorticalbox said:

Lucky you never hitting full ram, at work I had to be upgraded from 8 to 16 because my work load was hitting 8gb and use a swap.

 

Tbf I do a network of docker containers that mimic a small part of infrastructure in aws, however you should always have some swap just in case. 

I can call myself almost advanced linux user. I know what can happen but i have some cautions taken to prevent bad things from happening. Limited process count, prevent fork bombs, ram usage limits :D Limit user niceness level so that root can save the system when unresponsive. You can do so much things on linux. You just have to imagine and all those things can be done under linux. You can limit cpu for program and many more things :)

Computer users fall into two groups:
those that do backups
those that have never had a hard drive fail.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, mate_mate91 said:

I can call myself almost advanced linux user. I know what can happen but i have some cautions taken to prevent bad things from happening. Limited process count, prevent fork bombs, ram usage limits :D Limit user niceness level so that root can save the system when unresponsive. You can do so much things on linux. You just have to imagine and all those things can be done under linux. You can limit cpu for program and many more things :)

CONTAINERISE ALL OF THE THINGS \o/

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, mate_mate91 said:

I can call myself almost advanced linux user. I know what can happen but i have some cautions taken to prevent bad things from happening. Limited process count, prevent fork bombs, ram usage limits :D Limit user niceness level so that root can save the system when unresponsive. You can do so much things on linux. You just have to imagine and all those things can be done under linux. You can limit cpu for program and many more things :)

Yeah one day I will sit and learn all this stuff. My work laptop runs linux. Linux in product a d linux in development.

 

1 hour ago, ViscountStyx said:

CONTAINERISE ALL OF THE THINGS \o/

It's what I do at work lol, node/express app and run that in docker then deoy to aws.

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, vorticalbox said:

Yeah one day I will sit and learn all this stuff. My work laptop runs linux. Linux in product a d linux in development.

 

It's what I do at work lol, node/express app and run that in docker then deoy to aws.

 

 

I'm working on a containerised SSH project at the moment - as each session will require space to work with.  Docker is doing my nut in.  Essentially, each SSH connection on the port specified should spawn a new container etc etc.  

 

I've considered starting drinking again.

Link to comment
Share on other sites

Link to post
Share on other sites

I think it is a common misconception (to a degree) that swap (be that a partition or a file) is used to protect from running out of RAM. That (may) have been the use case previously (EDIT: back in the days when RAM was measured in MBs, not GBs), but even then it was usually the case: If something starts to go haywire in so much that swap gets bombarded, the system is probably nearly-nonresponsive already and on it's way to crash / become useless.

 

But, the above assumption is not true. (About protecting from running out of RAM, not the latter part). Actually, swap is still useful to move those applications and their data that are in RAM, but are not used for long periods of time, away from RAM to free up space for file system buffers. In common desktop usage, you would rarely notice the difference, but it doesn't hurt to have swap space. And - in some use cases - it might actually make things run smoother (even if you have loads of RAM, if you ever use, say, 30-50%, of it at any one time for running applications). In case you are not happy with the default behavior, you can even adjust swappiness on Linux systems.

 

The rationale is: think about your use case(s) and get enough RAM for it. Never hurts to have swap, although usually, you can do quite well even without it (think about swap as a small tweak to increase performance / loading times in corner cases).

 

Also, @jpenguin had a good point; if you are going to need / use Hibernation, it makes sense to have swap, since it can double for both.

 

See: https://haydenjames.io/linux-performance-almost-always-add-swap-space/

EDIT: Acutally, Ubuntu FAQ is better than the link above, at explaining why swap might be useful, even if you have quite a lot of RAM:

Link to comment
Share on other sites

Link to post
Share on other sites

My personal experience with swap, both on Windows and Linux, is that it is 1000x worse than just running out of RAM and having the OOM killer do its job.

When the system runs out of ram, and you are not using swap, the OOM killer runs and kills the programs using the most memory until your programs can fit within the memory you have. This is usually instant and frees up the system with almost no delay, though you will likely lose whatever that application was working on.

 

With swap, I found that every time it has needed to be used, the system grinds to a complete standstill. When eventually (like 10-15 minutes) I can run `kill -9` on linux I have to run `sudo swapoff -a` anyway to clear everything that doesn't want to leave swap now that there's free space because otherwise the system has a temporary freeze while trying to open the volume control widget...

 

On windows, it's even more catastrophic; I almost always had to hard reboot the system whenever windows ran out of RAM because it never started responding again.

 

Things might be different if you have an nvme or something for swap instead of a hard drive, but to me it's just not worth devoting disc space to.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, pipnina said:

My personal experience with swap, both on Windows and Linux, is that it is 1000x worse than just running out of RAM and having the OOM killer do its job.

When the system runs out of ram, and you are not using swap, the OOM killer runs and kills the programs using the most memory until your programs can fit within the memory you have. This is usually instant and frees up the system with almost no delay, though you will likely lose whatever that application was working on.

You're not alone. I've seen this experience mentioned often on the net. It has happened for me, too, but that was like... 10 years ago? (On Linux). I guess I just use more stable software now (that doesn't have huge memory leaks).

 

Actually, the last time I had a crash because of a leak, was (around 8-10 years ago) while I was playing Blobwars: Metal Blob Solid. It had a nasty bug (incompatibility with some library) that caused a grazy leak, like 500MBs in 10 seconds or so. Back then I (maybe) had 1GB of RAM :D

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Wild Penquin said:

You're not alone. I've seen this experience mentioned often on the net. It has happened for me, too, but that was like... 10 years ago? (On Linux). I guess I just use more stable software now (that doesn't have huge memory leaks).

 

Actually, the last time I had a crash because of a leak, was (around 8-10 years ago) while I was playing Blobwars: Metal Blob Solid. It had a nasty bug (incompatibility with some library) that caused a grazy leak, like 500MBs in 10 seconds or so. Back then I (maybe) had 1GB of RAM :D

For me, it's making a programming mistake of my own (I once accidentally made a linked list that didn't stop, that ate my ram pretty quick).

And a year or more back, it would have been underestimating how much memory blender will need when I hit "Bake"

Link to comment
Share on other sites

Link to post
Share on other sites

On 2018/8/5 at 11:50 AM, ViscountStyx said:

If you don't want to make a disk partition - you can use a swap file instead that will grow as needed (much like the windows page file).  Doesn't need to be big - hell even 128MB would be suffient for those odd packages that require swap. 

 

On 2018/8/5 at 3:41 PM, vorticalbox said:

Thanks. I'm using swap file now~

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 months later...

Swap is useful for many reasons.   Think of it more like tiered memory of a hard drive to an SSD, if you have computed data you don't necessarily need right now but would cost more to recreate, the storing that in memory is advantageous.  Sure you could create an overlay and mmap() it too.

 

The fun thing is when swap resides on fast(er) storage like an SSD or nvme.   It's not as fast as RAM but a hell of a lot faster then a hard drive.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/2/2018 at 3:30 PM, happymax1212 said:

I want to install Linux Mint on my laptop which has 8G of RAM. I know Swap is sort of virtual memory, and I don't think using my poor 5400rpm HDD as virtual memory can improve performance. Do I need a Swap partition? If so, how much storage should I spend on it? Or is there an alternative similar to the pagefile in Windows rather than a partition? When I last used Linux I didn't really care and just gave 16G for Swap, but I don't have that much storage on my laptop.

Yes, for some intensive programs it is needed even on a modern OS. If you are rendering a very big video with kdenlive or ffmpeg for example, you system can freeze without a swap memory. Or even when using virtual machines

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Lukyp said:

Yes, for some intensive programs it is needed even on a modern OS. If you are rendering a very big video with kdenlive or ffmpeg for example, you system can freeze without a swap memory. Or even when using virtual machines

The system will not freeze when running out of memory. The system runs the OOM killer when a program requests more memory than the system has available. Under that situation it kills the programs taking the most memory, so if you had a virtual machine that was using 8GB of RAM and the OOM killer is called, that virtual machine is going to be removed.

Unless you have at least an SSD, the OOM killer is a better solution IMO. Using more RAM than you physically have by using drives (especially hard drives) will only serve to make your system more and more painful to use the more data you have stored on the disk.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, pipnina said:

The system will not freeze when running out of memory. The system runs the OOM killer when a program requests more memory than the system has available. Under that situation it kills the programs taking the most memory, so if you had a virtual machine that was using 8GB of RAM and the OOM killer is called, that virtual machine is going to be removed.

Unless you have at least an SSD, the OOM killer is a better solution IMO. Using more RAM than you physically have by using drives (especially hard drives) will only serve to make your system more and more painful to use the more data you have stored on the disk.

In fact it never happened for me with virtual machines, even if the memory was being used a lot, but for a 4k 120 fps with kdenlive even the OOM killer was not enough (I was not running a swap partition) after 30 minutes it was still frozen even when issuing REISUB, well It was a try because I just have got 8 gigs of ram lol, tried again with a swap partition, still frozen but it was responsive to terminal commands

So Imo it should always being configured at least to prevent data loss for forced shutdowns in certain workloads/situations

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×