Score:2

How to auto move swap back to RAM?

cn flag

My original problem with swap(piness) was that it was swapping too often (even with swappiness=1! ).

I also thought 0 would turn off the swappiness - which was a misconception,
Swap still works even if you set sudo sysctl -w vm.swappiness=0 ! :-)
(To set permanently, edit /etc/sysctl.conf and add/update vm.swappiness = 0 - for GUI text editor I recommend a "linux notepad" "gedit", you can then do sudo gedit /etc/sysctl.conf)

Only with swappiness 0 it started to behave more like one would expect (swapping starting at about 98% of RAM full), however I am still experiencing one more problem - and that when enough RAM is actually freed again, the swap does not automatically go back to RAM, not even over time.

Of course you could manually do sudo swapoff -a; sudo swapon -a every time which turns off the swap which ultimatelly forces the swap to be emptied out to the RAM and then swap gets started again.

However that's unhandy for 2 reasons a) the swap gets turned off for a moment b) it's a manual extra checking/running

Is there a way how to achieve this in some, preferably "native", way automatically?

Raffa avatar
jp flag
For automatic check: `[ "$(free | awk '/Swap:/{print $3}')" -lt "$(free | awk '/Mem:/{print $7}')" ] && echo "Free memory more than used swap" || echo "Free memory less than used swap"`
user535733 avatar
cn flag
The kernel does indeed return frequently-used swap pages back into RAM. It doesn't return rarely-used pages quickly...because they are rarely used, so swap is an appropriate location for them. If you are using a RAM-constrained system, don't view swap as merely overflow for when the RAM is full. View swap as a different form of memory with different characteristics.
heynnema avatar
ru flag
Edit your question and show me `free -h` and `swapon -s` and `sysctl vm.swappiness`.
jave.web avatar
cn flag
@user535733 the issue is I have some stuff that I may not need right at the moment, but I need it to be ready when I go there - swap really is only an overflow safe break for me (so I have time to kill the right process when I start to shoot too much GBs into RAM)
Score:3
cn flag

Afaik, no setting is available to change the tendency to return swapped pages to RAM. You could, if you wish that, work with a script that checks RAM and SWAP, and turns swap off/back on when it is safe. On a memory limited system, that may be never: the only option there to clear swapped memory is to close down processes that own them.

It is not clear what the benefits will be for you to immediately move SWAP back to RAM whenever it is possible. Linux automatically manages the swap. If it does not release the occupied swap, it is because that swapped memory is not needed and thus can happily remain on the disk until needed. This way, ram and processor time are spared for use of the applications where it matters. Your disk is spared in that swap operations are minimized.

How fast swap kicks in depends on your swappiness setting, but also depends on the amount of RAM you have. You did not mention the amount of RAM you have. Based on your experience, one can assume that either you have little RAM (perhaps 2 GB or less), or that you are using specific applications that have unusual memory needs.

In the first case, you may need to change your computer use habits to account for the limitations of low RAM, until you can upgrade physical RAM. Swap is in no way a replacement of RAM, it is only a trick to extend it somewhat and allow somewhat more on a RAM limited system.

In the second case, then leave it alone: your system behaves optimally, swapping out unused RAM to leave it to your memory hogging work.

jave.web avatar
cn flag
the issue is I have some stuff that I may not need right at the moment, but I need it to be ready when I go there - swap really is only an overflow safe break for me, so I have time to kill the right process when I start to shoot too much GBs into RAM, which is something that happens or I even have to do quite regulary. (and I would not call it a trick) BTW: I feel like only the last paragraph is really answering the question. Also I see this trend of swap answers avoiding the inevitable - there is a problem that needs a solution (which may not be there yet, yet the problem remains). :-)
vanadium avatar
cn flag
The first part is to hint that what you consider the problem possibly is not the problem. In the end, the bottom line is: buy sufficient RAM for your needs. No other approach will really help you. Did you actually add some info about the amount of RAM you are struggling with? I see that in the mean time someone was kind enough to write the script for you that I mentioned.
vanadium avatar
cn flag
Moved the "answer" to the top ;)
jave.web avatar
cn flag
That's another trend I don't like - "buy X", I thought it was obvious (I'm sorry if it wasn't), if I was able to buy/upgrade to enough RAM, I would not be asking this. I'm sorry not providing the RAM info - there are 2 cases - 16GB & 32GB. Don't get me wrong I'm really glad you are willing to help :) just a bit disappointed in Internet's approach towards people that need solution for their current situation
jave.web avatar
cn flag
Anyways, for my case in particular, upgrading RAM would not be an option anyway since it does not have the same **behavioral** consequences(as I've described in my 1st comment) and the RAM overloads I'm catching with swap can be **virtually unlimited** :)
vanadium avatar
cn flag
I understand your need, but not the "why" of your need, and your question does not make that clear because you hardly document the specific use case that may lead to your situation. Unless someone comes up with better, as it currently stands, you need to implement a script yourself as I indicated in this answer. I see that you in the mean time accepted the answer of the person providing the script.
jave.web avatar
cn flag
Although I haven't document the use case I think I've descrbed pretty clearly the need and things I'm already doing which implies I've already gone through the common Q&As so I found the most of your answer off topic - to be clear - that's the reason why I did not accept it, **not** because the lack of code. But if you're interested - the generalized use case are random memory leaks/cache garbage generated by apps which I can't control at all but they do happen once in a while and when I'm generating stuff using RAM memory on purpose but with data that can contain unpredictability :)
jave.web avatar
cn flag
However thank you for your input, I don't want to sound ungrateful :)
Score:1
jp flag

If you must do it, then swapoff is the way to go. You can automate this process by implementing a bash script like so:

#!/bin/bash

sysctl -w vm.swappiness=0

while true
do
    total_m=$(free | awk '/Mem:/{print $2}')
    free_m=$(free | awk '/Mem:/{print $7}') 
    used_s=$(free | awk '/Swap:/{print $3}')
    total_m_percent=$(("$total_m" / 100))
    free_m_percent=$(("$free_m" / "$total_m_percent"))
    # Deactivate the swap and move pages to RAM if there is enough room in RAM and free RAM is more than 30%
    [ "$used_s" -gt 0 ] && [ "$used_s" -lt "$free_m" ] && [ "$free_m_percent" -gt 30 ] && swapoff -a
    # Activate the swap if the remaining free RAM becomes less than 5%
    [ "$used_s" -eq 0 ] && [ "$free_m_percent" -lt 5 ] && swapon -a
    # Sleep for 10 seconds to avaoid straining the system
    sleep 10
done

Run the script with sudo bash scriptfile.sh or add a systemd service to run the script automatically when the system starts.

jave.web avatar
cn flag
Looks interesting - so there is no way to just move the swapped stuff without swapoff ? :(
Raffa avatar
jp flag
@jave.web [Memory management](https://www.kernel.org/doc/html/latest/admin-guide/mm/index.html) is complicated and vital for the system … The Linux kernel is taking care of that very well… So the wise thing to do is just to `swapoff` and let the magic happen :) … “is there another way?” Sure… “is there a better way “ I doubt it … The script in my answer is especially cooked to address your requirement and it should run flawlessly as a `systemd` service.
muru avatar
us flag
Code review: when assigning a command substitution to a variable (`var=$(foo)`), quotes are not necessary around the command substitution (but they don't hurt). But when using a variable in a command, `[ "$used_s" ...]` the variable should be quoted.
Raffa avatar
jp flag
@muru Thank you for your code review, I totally agree with you on the concept of good practice... However, only numeric values are expected in those variables so quotes are not necessary around those variables (but they don't hurt)... I see your concern about `$used_s` but even with swap deactivated this variable will contain the numeric value of `0` ... Your opinion on this matter, however, has a heavier weight on the right side :)
Raffa avatar
jp flag
@muru I had to edit and add `[ "$used_s" -gt 0 ]` and `[ "$used_s" -eq 0 ]` to prevent unnecessarily issuing the `swapoff` / `swapon` commands ... I also found it a chance to sort out the quotes business on the way :) ... Appreciate your opinion and thanks again.
jave.web avatar
cn flag
@Raffa I was just wondering if kernel can move stuff from RAM to swap if it can do it the other way around in some way while keeping the swap on in case another huge memory starts to being allocated :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.