Wednesday, March 7, 2007

Memory management 1

Ok...so this is not really a "post" in the true sense. I am just pasting a series of mails amongst friends trying to address a memory management question that I had this morning:

-----------------------------------------------------------------------------------------------------------------

Email 1 -- Alpha
-----------------------------------------------------------------------------------------------------------------

OK...I accept that I am dumb and that I have no clue what is happening on my PC...but please help me understand this:

Below is a screenshot of my Task Manager...how can CPU usage be "00" when the process is using up 47 MB of my RAM ??? what am I missing here ??? If an app is using up memory, it must be doing something right ? why wd it need memory otherwise ? is there a way to make windows allocate memory only when I actually want the app to do something for me ?




-----------------------------------------------------------------------------------------------------------------

Email 2 -- Beta

-----------------------------------------------------------------------------------------------------------------
Ok occupying memory and doing something are totally different things. Let me explain.

Let's say you launch a program on your computer. All programs on your computer reside on your hard disk. It is very inefficient to run programs from your hard disk because it is very slow. So the computer tries to load the program into RAM, which is a quicker memory. Let's say you click a button on a window, the computer does the necessary action by executing the code that is loaded into RAM.

But let's say you have 512MB RAM and all of it is already filled up with 2 or 3 programs that got loaded into it. Now you launch a new program. What happens? Will it run from the hard disk? Actually no. It will use algorithms to figure out which part of RAM was least used (or last used based on algorithm) and swap that part of RAM (called a page) out of RAM and get a part of the new program into the RAM. So in reality, your computer is constantly swapping parts of programs in and out of RAM and executing the necessary parts from RAM.

So 47MB of RAM used up on your computer is taken up by programs that are loaded into your RAM, ready to execute. But is it necessary that they do some work when loaded into RAM? No it's not. Because, for example, you might have launched a browser, loaded a news website and reading the article. The program is not doing much at that time (except displaying the content). So the CPU usage is 0%. But the 47MB may be taken up by the browser program loaded into RAM.

In my view, in most of our home PC's, the CPU's are idle most of the time. Probably around 90% of the time. That doesn't mean that a slower CPU will do the job for you. The remaining 10% of the time when the CPU is busy, a faster CPU will do the job faster for you. So if you see the CPU usage graph, there will be small bursts of activity, with long inactive periods inbetween.


-----------------------------------------------------------------------------------------------------------------

No comments: