You are not logged in.

#1 2019-03-02 08:46:32

pkejr
Guest

Swap and memory questions

Hello,

I have a laptop with 8GB RAM and I often work with multiple things that are RAM consuming.
Since a year or so, I disabled my swap because I didn't want it to "kill my SSD". Knowing that, I tend to be careful with what applications I start, but it's often that I forget it and my RAM reaches 90% and then my laptop freezes.
Now I'm not sure this is real or if it's a myth, but anyway I decided to re-enable my swap.

So I have a single 8GB swap partition, which is inside my encrypted LVM volume. I did play with the `vm.swappiness` option, and I even tried to set it to 100, but I feel like swap is not working "as expected".
So after doing that, I started my usual workflow, starting some VMs, starting a compilation, some tabs in my web browser, until I reach 85% of my RAM. And until then, even with swappiness set to 100, when I type `free -h` the swap is never used.
It's only when I reach 90% or so, that it starts writing a bit of something into my swap (like 10MB, so roughly nothing). So in the end my laptop keeps freezing when I reach 95-100% of my RAM.
I managed once to  kill X and then free some memory and stop my laptop from freezing, but when I checked the swap usage, it was using only 60MB of swap so...

So I was expecting swap to prevent my laptop from freezing, and I was expecting swap to be used more than that, but that's not the case, so can anyone tell me why, is my configuration wrong, are my thoughts wrong?

Thanks

#2 2019-03-02 09:11:31

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Swap and memory questions

Hi,

Sorry to hear you're having problems.

Yes, it is possible to "get by" without swap. I tried it for some time too. I don't do that any more. Swap is okay.

Something to keep in mind is that there are all sorts of optimisation processes going on with how the kernel uses memory and swap. The kernel doesn't wait for all the memory to be exhausted before using swap, instead it will actively use swap to avoid memory fragmentation (analogous to file fragmentation in a file system). I.e. before all memory is exhausted, the kernel will begin offloading "less important" memory to swap in favour of more active processes. So, while, yes, swap is slow ... not using swap can end up slow anyway, because you are removing the kernel's ability to use the swap for optimisation.

Setting swappiness to 100 sounds like a bad idea. A swappiness of 100 tells the kernel to be very aggressive about favouring swap: "move all the things out of memory to swap".

One obtains a more responsive desktop system by lowering swappiness. If I understand correctly the default is 60. It's a sane enough choice. For a desktop/laptop you might lower it to 10 or 20. The only real way to be sure about the best value is with benchmarking. Personally I choose 40 because it feels like a nice compromise value between the safe default of 60 and the anti-swap extra-snappiness of 10 or 20. (I admit I have not tried benchmarking for this.)

I think you're better off focusing on what is using up all your memory. You must be running some heavy stuff to use up all that memory all the time. Go for a more a lightweight build (eg just use a window manager instead of a full-fledged desktop environment), and use the "top" command to monitor what is using all the memory.

If there is a heavy process which you really can't do without then the only viable solution is to upgrade your RAM. No sysctl magic will change the fact that your machine is subject to laws of physics and computer science. Accept the fact that 8G just may not be enough for some software.

Last edited by /dev/zero (2019-03-02 09:12:47)

Offline

#3 2019-03-02 09:12:47

bernd_b
Member
Registered: 2013-07-30
Posts: 183

Re: Swap and memory questions

In my experience when running out of memory the system slows down which might be experienced like a freeze. But in fact, before things get stuck my system (i guess the kernel) kills processes to regain the work of the basic operation system function.

Anyway, in the times I used swap I couldn't decide if it was a bless or curse. My swap was on a classic hdd and if it was not to export things in the memory which won't be used for a longer time it never occurs to me that swap could be an alternative to missing memory. It simply was too slow.

I now have swap deactivated. I either will buy additional memory or close some software if free memory runs low instead of suffering from the user experience with steady swapping.

Offline

#4 2019-03-02 11:56:46

mmdamin
Member
From: Iran
Registered: 2019-02-19
Posts: 205
Website

Re: Swap and memory questions

You can also use a file instead of a partition for swap.
if you see 60MB swap after killing proccesses, it may free swap while you were killing proccesses.
I use jetbrains, it uses 3GB of RAM (my RAM is 4GB) and I'll have 500MB free space on memory. also I have 8 GB swap, if I minimize jetbrains and open something else, my system slows down then freezes. often I use CTRL+Alt+F2 then login as root and restart lightdm service to recover system, it will kill all processes I had opened in GUI.

Offline

#5 2019-03-02 12:34:25

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,652

Re: Swap and memory questions

The critical question here is how exactly your RAM is loaded. The swap space exists to save "anonymous" pages (ie. process memory) in contrast to file caches (where the cached data is just dropped and eventually re-read from disk).
So if the problem is around file caches because something reads huge files into RAM, adding swap will have no impact and raising the swappiness would be contraproductive, since now the system will rather preserve (old) file caches and on top of the exisiting memory issues will constantly have to write/read process memory.

=> Figure the cause of the RAM drain and dump "cat /proc/meminfo" for a general overview on the situation.

Also there are multiple records on this forum (and elsewhere) for swap on LVM to be extra-slow (likely CPU load), the encryption doesn't seem to matter.
https://bbs.archlinux.org/viewtopic.php … 4#p1758134

Offline

#6 2019-03-03 14:34:11

pkejr
Guest

Re: Swap and memory questions

Thanks for the details.
In my case, I am not able to do CTRL+ALT+F2 after the laptop frezzes because it's... frozen! Also I don't need to know what's eating my RAM, because I already know it (make -j4, VM1, VM2, 40 web browser tabs, etc.) so thanks for the light build advices, but I'm already using a tiling WM with no desktop environment.

The thing is I feel like my swap is useless because it's never used (I tried playing with swapiness parameter, as said before, but the results are similar).

Also I was expecting my laptop not to freeze, maybe "slow down" because of the speed factor between ram and ssd, but my laptop is still freezing and I can't find a proper solution apart hard reboot.

But I guess you answered the question anyway, all I need to do is find a way to add more RAM to my laptop.
Thanks for the answers smile

#7 2019-03-03 17:20:24

mmdamin
Member
From: Iran
Registered: 2019-02-19
Posts: 205
Website

Re: Swap and memory questions

pkejr wrote:

Thanks for the details.
In my case, I am not able to do CTRL+ALT+F2 after the laptop frezzes because it's... frozen! Also I don't need to know what's eating my RAM, because I already know it (make -j4, VM1, VM2, 40 web browser tabs, etc.) so thanks for the light build advices, but I'm already using a tiling WM with no desktop environment.

The thing is I feel like my swap is useless because it's never used (I tried playing with swapiness parameter, as said before, but the results are similar).

Also I was expecting my laptop not to freeze, maybe "slow down" because of the speed factor between ram and ssd, but my laptop is still freezing and I can't find a proper solution apart hard reboot.

But I guess you answered the question anyway, all I need to do is find a way to add more RAM to my laptop.
Thanks for the answers smile

good luck.
In my case, only xorg freezes, and for tty, I get response too late (for example, I press 's' button and the character appears on screen after 5 seconds). I use a very heavy desktop environment (deepin).
You may pause VMs that you are not using them for the moment, or giving them lesser RAM. Also Try a lightweight browser. (chrome eats RAM, may you found a browser that is as light as lynx or elinks as graphical as others)

Offline

Board footer

Powered by FluxBB