11

Ubuntu 18.04

I keep having same issue - due to my bad habbits, my Firefox gets to open and hold in memory too many tabs. This causes RAM usage to eventually hit 100%
At which point whole system hardlocks. I can't even move my cursor to press "end process" in system monitor.

Same behavior appears when I run my code and take too much memory - everything stops working, mouse becomes unresponsive.

My guess is obvious - shortage of free memory blocks Gnome or Ubuntu itself.
So my guessed solution would be to make those OS systems reserve memory to always work and respond.

How do I do that?


as requested

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7,7G        4,3G        281M        1,2G        3,1G        1,9G
Swap:          2,0G        1,5M        2,0G
$ sudo sysctl vm.swappiness

vm.swappiness = 60
Noone AtAll
  • 396
  • 1
  • 4
  • 15
  • 3
    Normally there is some swap, in 18.04 LTS usually a swap file. Check with swapon. When swapping, the system will be very slow, but it will still work. When you run out of swap, the system will get no longer work. So you may need more swap space, but the best option is to get into the habit of closing tabs in the browser. If you really need more memory to run your own program, you should get more RAM. – sudodus Oct 13 '19 at 14:20
  • Imo, if OS allows to be hardlocked by programs it runs, it's OS' problem, not user's.

    If it leaves solving the problem up to user, then it should allow user to disable programs even after running out of space (by actually responding)

    – Noone AtAll Oct 13 '19 at 14:27
  • 1
    Are you sure that the whole operating system is locked, not only the program that is locked, or maybe only very very slow (due to swapping)? – sudodus Oct 13 '19 at 14:29
  • System doesn't respond to moving or clicking mouse. That's as close to hardlock as it gets in GUI – Noone AtAll Oct 13 '19 at 14:33
  • Well, things are not perfect. I have no better explanation. Let us hope that somebody else can chip in and help you. – sudodus Oct 13 '19 at 14:37
  • 2
    It's worth a try as a quick test, increasing /proc/sys/vm/min_free_kbytes. Otherwise, I agree with @sudodus ' comments. – Doug Smythies Oct 13 '19 at 15:20
  • 1
    When such "hardlock" occurs, just press Ctrl+Alt+F3 and try to login to text only screen. If you log in (it may be slow), you can detect the process that is stuck by some commands like top, htop, etc. (Note: To go back to your graphical environment use Ctrl+Alt+F2.) – FedKad Oct 13 '19 at 16:01
  • 2
    https://unix.stackexchange.com/questions/28175/system-hanging-when-it-runs-out-of-memory – DK Bose Oct 13 '19 at 16:14
  • 1
    Edit your question and show me free -h and sudo sysctl vm.swappiness. Start comments to me with @heynnema or I may miss them. – heynnema Oct 13 '19 at 17:48
  • @heynnema added – Noone AtAll Oct 13 '19 at 18:03
  • 1
    Your swap is too small. Do you use a swap partition, or a /swapfile? Show me cat /etc/fstab and a screenshot of gparted. – heynnema Oct 13 '19 at 18:13
  • Let's talk about the elephant in the room - the Linux kernel's inability to gracefully handle low memory pressure -- https://www.reddit.com/r/linux/comments/cmg48b/lets_talk_about_the_elephant_in_the_room_the/ – Elias Oct 13 '19 at 18:16
  • @Elias There is OOM-Killer for that on the kernel level. However user space script can also deal with the issue. – WinEunuuchs2Unix Oct 13 '19 at 22:22
  • 1
    Rather than reserve memory for the system, limit the user. See the cgroups accepted answer at https://unix.stackexchange.com/questions/34334/how-to-create-a-user-with-limited-ram-usage – ubfan1 Oct 13 '19 at 22:54

2 Answers2

4

Instead of a thousand words: system hang prevention with nohang: https://youtu.be/PLVWgNrVNlc

Nohang is a highly configurable daemon for Linux which is able to correctly prevent out of memory (OOM) and keep system responsiveness in low memory conditions: https://github.com/hakavlad/nohang

But easiest way is installing earlyoom. This program is in the repositories starting with ubuntu 18.04: just run sudo apt install earlyoom.

Alexey A.
  • 41
  • 1
0

All you really need to do is close a FireFox tab or two before you hit 100% RAM. But how do you know when you approach that point?

You can have a script running that pops up a notification bubble at 95%:

What I do is have conky always running showing me my system status:

conky 30 second clip.gif