Why system still hangs/slows after process is finished?

2

2

My OS is Ubuntu 12.10 64 bit. I have experienced this issue for a long time, with all previous versions of Ubuntu as well. If I launch a process, which takes up all the memory and when finished, the computer is still paralyzed meaning is not as fast as it was before starting the process.

Is it a common scenario and what can be done to resist/cure it?

Sukhi

Posted 2012-11-27T18:59:53.967

Reputation: 123

Answers

2

First, you probably shouldn't be running a process that takes up all the memory.

The reason this happens is due to swap space.

Analogy Time

You have a bus. This bus has "preferred" seats inside the bus, and "overflow" seats on the roof. People only sit on the roof if all the preferred seats are taken and they still really need to ride on the bus.

This bus has the following unique properties:

  • The seats are extremely small; an average person would occupy a great number of seats, maybe 2 or 4 for a child, 8 or 10 for an adult, some as many as 40.
  • The time required to board or disembark the bus is 20 times faster for people in preferred seats than overflow seats.

There are also three cultural rules about this bus:

  • One rule is that people who are just boarding the bus can "bump" people who have been riding the bus earlier, which forces that person to get up from a preferred seat, and move up to an overflow seat on the roof. Everybody is used to this behavior, so it doesn't cause any fights or misunderstandings; it's just part of the culture.
  • The second rule is that, if the bus is full, then the person occupying the most seats on the bus must leave immediately when someone tries to board the completely full bus.
  • The third rule is that, whenever a preferred seat becomes available, someone who might be in an overflow seat will immediately get up from the roof and come down to sit in the preferred seat.

Now, imagine you fill up all the preferred seats. Then more people want to get on the bus. So someone gets on the bus, and all the preferred seats are taken, so by cultural custom they force someone sitting in a preferred seat to climb up to the roof. This process can repeat until even the overflow seats on the roof are full.

Once all the preferred and overflow seats are full, what happens? The largest person has to get off the bus. If they are in a preferred seat when they leave, they can leave quickly; but someone who is smaller (or multiple people) can come down from the roof, by the rules, to sit in the available preferred seats.

Now, to apply this analogy to reality: the "overflow seats" are swap space on the hard disk drive, and the "preferred seats" are RAM. The rules of the bus apply pretty much cleanly.

What happens is that when your "large person" (the process taking up lots of RAM) is killed, the system starts moving processes that had been in swap space (the overflow seats) back into RAM (the preferred seats). This is because it is much faster to board and disembark the bus from the preferred seats, and people are always getting on and off, so you don't want to have to wait at the stop for a long time while people get on and off from the roof.

Virtual memory operating systems use up as much RAM as possible at all times. The highest priority usage is for actually mapped data from programs that are actively executing and accessing those segments of RAM. Medium priority usage is for mapped pages for programs that are not actively executing. Lowest priority usage is for "page cache", which is basically a cache of your hard disk drive's most frequently accessed files, so that they don't have to be read from disk every time they are accessed.

Most of the programs on your system, aside from the one taking up all the RAM, get shoved into swap space (the overflow seats) when your big program takes up all the RAM, so when you end the big program, everything starts being moved back into RAM so that your desktop remains responsive. While this transfer is going on, since it is very slow, your system bogs down, because it has to load balance I/O requests from the virtual memory manager and I/O requests from applications. That's the more technical definition of why this happens.

The "cure" is three-fold:

  • Priority One: buy more RAM. This can never hurt you and can only help you. If you've got the money, and your motherboard has the capacity to hold more RAM, do it.
  • Priority Two: don't run that program that takes up all your RAM.
  • Priority Three: reduce the size of your swap partition, or eliminate it entirely, or buy a faster hard drive (such as an SSD).

allquixotic

Posted 2012-11-27T18:59:53.967

Reputation: 32 256

Thanks, nice answer. So, when the swap space is getting freed by pushing the previous processes back on to the RAM, will the system be back up to its original state (speed) or there will always be some loss in that meaning it can never recover completely. – Sukhi – 2012-11-27T23:37:37.910

It will recover over time as pages are migrated from swap back to RAM. – allquixotic – 2012-11-28T02:27:24.087