How to reduce RAM usage without a reboot?

My Manjaro virtual machine has 5 GB of RAM (and matching size swapfile) and runs fine unless I run qbittorrent.

If I run qbittorrent, the following happens.

  • At some point (e.g. after a couple of days’ usage), the system is sluggish (e.g. Firefox takes a long time loading a new page). Task Manager says 96% plus RAM usage. HTOP says qbittorent and Firefox are the two top consumers of RAM.
  • I close both qbittorrent and Firefox. But Task Manager’s 96% plus for RAM is unchanged. Also Firefox, restarted, takes a long time loading new pages.
  • I reboot the system. I start qbittorrent with the same number of “running” torrents (mostly not many). I start Firefox and load it with the same Web pages (i.e. the same that I had when the system was sluggish). But the system now runs fine. RAM usage is reported e.g. 48%. Firefox is quick about loading new pages.
  • Then after a couple of days, the whole sluggishness problem recurs and I have to reboot again.

Main question: Is there something I can do (e.g. running of commands) that will fix the sluggishness (technically, bring RAM usage from circa 96% to c. 48%) without a reboot?

I guess other questions might include: Why does this gradual build-up of sluggishness happen? Is there any way to prevent it? Why isn’t RAM released when I close qbittorent and Firefox? And perhaps other better questions I couldn’t think of.

But the main thing is how to reduce the RAM usage without a reboot. (I am afraid, “Why does it happen,” may be like, “Explain how the computer works.”) Thanks.

1 Like

Hi @jamjar,

While I have no idea how good an idea this is, or anything like that, you can check out this page:

There is also:

II take no responsibility if something breaks.

P.S.: You might want to invest in some additional RAM, since nowadays 5GB isn’t a whole lot.

Hope this helps!

2 Likes

you’ll have to install more RAM if you wanna run such memory-intensive applications. a temporary fix that you should do is to extend the swap-size (double or three times the ram). that should prevent freezing due to the lack of running out of memory/space. nevertheless it is no solution that will satisfy because swap is always a way slower than real RAM.
16 or even better 32 GB should solve the problem and it has become affordable nowadays.

1 Like

you specifically mention qbittorrent.

the linux filesystem caches files so at some point you will see a high memory usage.

you could flush the filesystem cache to see if it makes any difference

echo 3 | sudo tee /proc/sys/vm/drop_caches

The Linux disk cache is very unobtrusive. It uses spare memory to greatly increase disk access speeds, and without taking any memory away from applications. A fully used store of ram on Linux is efficient hardware use, not a warning sign.
Experiments and fun with the Linux disk cache

3 Likes

Let’s see your htop when that sluggishness happens. My guess it’s your swap that bottlenecks since virtual drive (well, even your physical drive, even when it’s a SSD) is way slower than RAM. Both due to speed and physical distance from CPU.

Same answer as above: swap.

  1. Don’t eat up all your RAM
  2. Don’t use swap (let the OOM killer works if your system is out of RAM)
  3. Give more RAM

They’re actually released but the cached pages stay cached, so that if it’s an accidental close or you reopen in a very near future without opening other apps that consumes much RAM (and kicks that cached pages out), it will open fast.

2 Likes

Thanks. Also thanks to Mirdarthos for links to similar commands.

I’ll have to wait for sluggishness to build before I could try that solution. Will report back to page on the outcome.

1 Like

Set up zram:

https://wiki.archlinux.org/title/Zram#Using_zram-generator

and set zram-size = 4096

1 Like

Close programs you’re not using (anymore).
IMHO fiddling with filesystem caches etc is overkill, linux will drop them if programs start using memory.
Also see

https://www.linuxatemyram.com/

1 Like

Linux usually handles memory quite well, including returning memory to free pool when applications shut down.

There are some points that might be checked:

  1. Almost always after some uptime RAM is used up to nearly 100% for file caching. This will be freed when there is need of RAM for other purposes.

  2. Firefox tends to continue to run without a visible window for shutdown activities. This should end after few seconds, I had it once in while that this process didn’t come to an end.

  3. Double check, that closing qbittorrent indeed quits the whole program. This kind of application likes to stay in the background to continue sharing and therefore continues to consume the memory.

you get a comprehensive list of processes using:

sudo ps xfa

1 Like

what @zbe said - it’s probably better to do this on the host rather than the guest
(we don’t know what your host is, this advice is for when the host is linux as well)

also, RAM usage isn’t the problem, the amount of available RAM is (swap included here)

1 Like

Thank you. That’s very useful to know. In that case, it would appear that the e.g. 96% RAM usage reported by Task Manager or HTOP is no indicator of how “hard up for RAM” my system is (because RAM used for caching is just like RAM unused in being ready for use by an app that needs it).

Question: If so, how do I find out what is the non-cache RAM usage (total RAM usage minus RAM used for caching)?

Question: Is there a way then to make an app “really” quit? Would I use your sudo ps xfa, identify the relevant “undead” process, and use kill? I suppose it may leave some files in an unusable state? (E.g. files being downloaded in qbittorrent in a state that cannot be resumed.) I wouldn’t care if Firefox must start from scratch.

There is command line tool, which called free.

free -h

Exemplary:

               total        used        free      shared  buff/cache   available
Mem:            30Gi       7.8Gi       1.3Gi       1.0Gi        23Gi        22Gi
Swap:           34Gi       9.8Mi        34Gi

Actually used are just 7.6 GB, but the rest is used for buffer/cache. So it appears to be full, but it is not when you see the color here (htop):

grafik

7.8 - 1.0 ~ 6.38

However that are not exact numbers, but rather estimated and rounded values.

If you kill a process, then the RAM Cache of it will not be purged. It will stay as an “undead” zombie. It can be reactivated anytime, but also overwritten by another process anytime.

If you want to drop the cache, then do as @linux-aarhus suggested:

But it wouldn’t make any real difference in my opinion, since it needs to cache all data again and should only be used for debugging purpose.

# Example
kill $(pgrep qbittorrent) && sync && sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'

If you don’t care about the current state, then just restart the display-manager:

sudo systemctl restart display-manager.service

That would be a “soft-reboot”, since only the GUI restarts here. It is pretty much the same as killing explorer.exe on Windows.

Thank you for that and the rest of your answer.

But I don’t understand how display relates to the current state.

More specifically, I understand from other comments that Firefox and qbittorrent may run “in the background” or without any visible window. If I were to restart the display-manager, would that have anything to do with those invisible “background” processes?

I just thought of the form of question I should have used perhaps: Is there a way to tell a particular app, e.g. qbittorrent, “Do the thing you would do if I were to reboot the system”?

The “display-manager” is lightdm, SDDM, GDM and what ever. When you restart it, then it will kill the session (the desktop environment) which were started by that and prompt for a login. That means: Anything what was started by the logged-in user will be terminated.

As said, the only difference would be the RAM-Cache. So if you drop the cache while qbittorrent is running: Only “undead” zombie data will be dropped, which has no relation to qbittorrent. When qbittorrent is terminated, it will drop the RAM-Cache after time or on request force-ably as explained.

For example: Qbittorrent relies heavily on disks like any file-sharing app. When this app requests a (piece of) file, it gets loaded into RAM and stays there until qbittorrent release it for overwriting. Any change of the file happens first in RAM and then gets synchronized with the file system. Now imagine a lot of disk requests by bittorrent… you know what happens.

Use more RAM or limit it. A rough, but practical way would be using cgroups.

Example with systemd:

systemd-run --scope --property='MemoryLimit=500M' firefox 

But note, that firefox will crash, if it allocate more memory than linux allows here. A more rough way:

#!!! Not tested, just an exemplary view. Only temporary. !!!
sudo mkdir /sys/fs/cgroup/memory/firefox
sudo sh -c "echo 500M > /sys/fs/cgroup/memory/firefox/memory.limit_in_bytes"
sudo sh -c "echo $(pgrep firefox) > /sys/fs/cgroup/memory/firefox/tasks"

You could force the kernel into dropping the cache and flushing the buffers… :arrow_down:

su -c "sync && echo 3 > /proc/sys/vm/drop_caches"

… or as an alterative… :arrow_down:

sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

… but that’s only a patch for the bleeding. Your problem is that…

  1. you keep your browser open full-time; and…
  2. your virtual machine only has 5 GiB of RAM available, which is too little for that type of usage.

Caching and buffering. Unused RAM is useless RAM, so the Linux kernel manages virtual memory in such a way as to make the system work as efficiently as possible. This includes caching.

It is cache memory, and it will be released when you start something else, but kept in memory if you do not, just in case you want to fire up firefox and qbittorrent again.

Yes. :wink:

Thanks. I know you like seeing posts resolved and closed. On this one, having to wait for sluggishness to build and to apply one method after another will take me some time. But when I have done that, I will report back with results and choose a solution. I can easily give more RAM to this VM but want to try the other ideas as a means of learning. (By the way, you were right about my keeping the browser open at all times.)

1 Like

To end the processes there should be a “normal” way. Like with qbittorrent, after closing the window there is still a symbol in the taskbar. To really end the program use the menu to quit the programm.

Just to answer your questions:

You can use ps xfa to find the process numbers. Use the number to apply the kill command. First ask friendly to terminate by simply
kill #number#
If this does not help
kill -SIGKILL #number#

Even easier is killing by name:
killall qbittorrent
will ask all processes with the given name to terminate.

As with kill you can also add a signal to force kill:
killall -SIGKILL qbittorrent

The SIGKILL does not wait for any shutdown procedure to finish, so there might be dataloss on open files if there was writing in progress.

An update.

I am finding it difficult to reproduce my problem artificially, e.g. by opening many tabs on Firefox playing 4k video until RAM usage reaches 96% plus percent and the loading of any new page becomes sluggish. With that much I succeed. But when I close Firefox, RAM usage rapidly drops to below 40%. When I restart Firefox, the sluggishness is gone.

Remember that my problem was that closing Firefox (and qbittorrent) neither reduced RAM usage nor restored speed of loading to Firefox when it was re-started.

From the above, I could deduce two things: 1. The problem is not just using up almost all RAM but involves an extra element, let it be x. 2. Something about my normal usage causes x over time.

Maybe I have to fill up the RAM with heavy usage in both Firefox and qbittorrent. Will keep trying.

The first of the above commands worked. (I do not as yet know whether the second would also have worked.)

By “working,” I mean ending the sluggishness.

In more detail: I see that my system has reached sluggishness “naturally” (noticeable in the loading of new pages in Firefox) > quit Firefox > see that RAM usage did not go down > restart Firefox > see that loading of pages is still slow > quit Firefox > run the command > see that RAM usage did not go down > restart Firefox > see that loading of pages is normal speed.

So RAM usage and sluggishness do not always correlate.

The command is good enough as solution for me as I wouldn’t have to run it very often. (The sluggishness may take a couple of days to build when it does. But when it does not, the system runs fine for weeks.)

I will let sluggishness build again and try the second command before choosing a solution and having the page close.

This is quite weird, as the RAM used should be freed when the using program is closed.

So it sounds like long uptime&churn causes some other levels of the system to allocate RAM. If this happens for instance in glibc then you would need to restart to reclaim it. But it would be a memleak bug in glibc…

Have you done a detailed assessment of what is actually holding the RAM?

1 Like