2

I've been often annoyed on Ubuntu when there's a peak in RAM -- for example when starting Android Studio's emulator or when a Chrome tab freezes -- the whole GUI freezes and so I'm forced to either wait for it come back or go to TTY and kill the culprit.

Is it possible that Ubuntu manages RAM very differently than Windows does? On Windows, the app itself freezes but not the whole GUI. Is there a way to make Ubuntu behave more like Windows in that respect?

wjandrea
  • 14,236
  • 4
  • 48
  • 98
Daniel
  • 153

2 Answers2

3

Modern OSs use paging to manage memory. It gives some advantadges to the system like being able to use more memory than you have physically, securing some sensitive memory area (like the kernel reserved memory) amongst other things that I can point some reference to if you want to learn more about paging.

But what is paging?

I'll skip the technicals and give you an analogy. Imagine you have an encyclopedia collection, A to Z, and being a curious person you like to spend afternoons reading it. You keep all of the encyclopedias on your bookshelf and you have a reading table which you can have 4 books on.

One day you are reading about "Sailing", which is on the letter S encyclopedia. At some point you might need to read about something else related, like "Winds" on letter W book. As long as you have it on your reading table you can quickly open it and read what you need, then go back to the "Sailing" book. Problem is, since your reading table can only fit 4 books, the chances are high you will need a book that isn't there already. Then you would need to take one of the books from the table back to the shelf, grab the book you need to the reading table and read it.

Ok, what does this whole book story has to do with my freezes? Well, the computer does something similar all the time. To be able to use more memory than it physically has, the computer divide memory in pages, which are small continuous blocks of memory. Those don't need to be on RAM (the "reading table") all the time, they might be stored on your disk (the "bookshelf"). If the RAM is full and the CPU needs to read/write on those pages stored on disk, one of the pages on RAM will be stored on disk, and the page requested will be loaded on RAM.

This is a Kernel job, so it is transparent to the user and programs. But since reading and writing to the disk is much slower than reading and writing on RAM, you will notice those freezes.

On Linux, there is a partition called swap, which is used to store those extra pages. Windows does the same thing, but I think it uses a file. The system kernel itself won't freeze, since it will be on locked ("non-swappable") pages.

The image below (from an wikipedia article) illustrates the concept of paging (and virtual memory, which we don't need to go into). Those blocks represents the pages being allocated on both memory or disk. Virtual Memory Paging

I believe the reason it might look like Linux is freezing too, is because Unity (the desktop GUI) might experience the delays caused by this swapping, while on Windows they might keep the desktop GUI code in locked ("non-swappable") pages. This is just a brainstorm theory though.

IanC
  • 931
  • Well that theory does actually make sense with the problem I'm having. I feel like Windows keeps every software used on its own page whereas Ubuntu seems to mix it all together. When a software is "Not responding" like it says on Windows, it becomes white but the others are still usable. Is there a way to make Ubuntu manage Unity differently then? Give it its own space or something? – Daniel Sep 29 '16 at 14:45
  • 1
    @Daniel, You misunderstood something. On both Windows and Linux, each software will require its own pages (pages because depending on the page size, most programs will require more than one). Those pages can be on memory or on the disk. If a page that is on the disk is required, it will be transfered to memory (maybe replacing some less accessed page) and then be used. This transfering cause delays on the processing because secondary storage (HD) read/write operations are way slower than memory. – IanC Sep 29 '16 at 18:44
  • 1
    @Daniel, About the theory I made at the end, if it's true there's not much you can do. You could try to edit the source code of Unity to lock the process on memory, but I'd not recommend it, since you would make that portion of memory unusable to other processes and might end up creating a bigger problem (maybe your Unity wouldn't freeze, but other processes would more frequently). Also, it would be a quite complex hack to do.. Best thing to do is tune the vm.swappiness to a good value and get used to rely on the command line in some more persistent freeze. – IanC Sep 29 '16 at 18:49
  • Oh I understand what you mean. It's a shame but I guess, until 16.10, I'll try another GUI see if I get the same issue then. I like Unity a lot but I'll try Cinnamon or something. Thanks! – Daniel Sep 29 '16 at 19:54
  • 1
    If you do want to try a last thing, this answer on stack overflow shows a way to use GDB to lock a process memory on RAM. Problem is Unity uses multiple processes and I'm not sure which one would have to be locked, neither if it would work. But I don't think it would harm to try something with it (if you're willing to change GUI because of that). – IanC Sep 29 '16 at 20:08
  • Maybe opening a gnome-system-monitor instance and locking it to memory using the steps from the answer. You might have some trouble switching back to it, so you could try locking the unity panel too. A bit sketchy, but could work. – IanC Sep 29 '16 at 20:18
  • Thanks for the tip. Indeed I will try it before changing. But I don't really have a choice given that it can sometimes lock up for 5 minutes. Which, when it happens several times in a day, definitely affects my productivity! I'll tell you if it solved the issue. Thanks! :) – Daniel Sep 29 '16 at 20:20
1

I had similar problems in the past. I changed swappiness of ubuntu in order to make the OS write RAM to HD much later.

Read an answer to change swappiness here: How do I configure swappiness?

I can not guarantee this helps to solve your problem, but in my opinion it is the best bet.

mondjunge
  • 3,296
  • 1
    I totally forgot about the swappiness kernel configuration! He could fine tune the system to make a better performance usage of swap (I actually think I need to adjust my own systems swappiness). I'm still curious on why the GUI on the OP Windows didn't freeze while Unity did. Does that locked page theory makes sense? – IanC Sep 29 '16 at 11:10
  • I tried that also. I get the same result with swapiness at 60 (default), 10 or 0. – Daniel Sep 29 '16 at 11:13
  • 1
    @Daniel, if the freezes are being caused because you ran out of RAM, the swapiness value will only affect at which point disk will start being used to store pages, but it will eventually happen and you will notice the delays because of the disk slower read/write operations. – IanC Sep 29 '16 at 13:10
  • The same happens with XFCE, not ony Unity. Also I observed that in a frozen state it is even not possible to change to another console (because keyboard is frozen) not its possible to acces the PC via SSH vom the network. In that state its not very helpful to know that the kernel inside is still running... – CatMan Sep 20 '18 at 13:07