I had created a program in C#. That program used about 60-70 MB of my memory.
But when I minimized that program, it required less memory, that is, just 10 MB.
When I maximized or came back to that program it used 20 MB...
Why does this happen?
Is it true that garbage collector gets called when you minimize the progam?
This has nothing to do with garbage collection - this happens with non .NET programs too (try minimizing your browser while looking at the memory footprint for it).
The moment you minimize a program, the windows OS will not longer need to keep the UI components in memory, hence the memory requirements are lower.
That is - windows trims the working set when an application is minimized.
See this channel9 thread and this KB article (thanks @Sasha Goldshtein).
The garbage collector runs whenever it decides to run; this is not necessarily tied to anything the user does, and certainly not minimizing the application. Generally, you can think of it as a function of the amount of memory usage compared to the total amount of free memory. But the point is, this is supposed to be somewhat opaque to you as a programmer. The big benefit of garbage collection over manually memory management is that you shouldn't have to worry about any of this.
I suspect from your question that you're using the Windows Task Manager to monitor your application's memory usage and determine when garbage collection occurs. This is a huge mistake. If you really need to do memory profiling, you need to invest in a proper profiler. Task Manager is not designed for this, and you'll often get false reads.
More specifically, the reason why you appear to see a marked decrease in the amount of memory consumed by your application when you minimize it is just one of the false reads you'll get when trying to do memory profiling with Task Manager. What's actually going on is that whenever you minimize an application, the Windows kernel automatically pages out a large portion of the memory it was using. You'll see this for all of your applications, not just those written in .NET. Because Task Manager is showing you the subset of the total memory in use by the application that exists in real memory at the time (i.e., the amount that hasn't been paged out to disk), it looks like the memory usage has decreased when it hasn't really. To get a slightly more accurate read, you should be looking at the process's "Private Bytes" value. This knowledge base article provides further details.
This article explains all:THE MEMORY MYSTERY
this is an extract from the above site:
.Net Urban Legend
It is possible to reduce the working set size of a .Net Windows Forms
application by minimizing and then maximizing the application
immediately after it loads. Windows OS trims the working set of
applications when they are minimized. The memory that was briefly used
while loading all those assemblies mentioned earlier is trimmed by the
process of minimizing and maximizing the application. You can
demonstrate this behavior to yourself by creating and running a
Windows Forms application with just Form1 and no added code.
Create and run the simple application.
Open the Windows Task Manager and then its Processes Tab. You will see the Task Manager shows your application’s memory usage at approx.
12.5 MB.
Now minimize your application and then maximize it. Check the Task Manager again. You will see the Task Manager now shows your
application’s memory usage at approx. 1.5 MB. The memory that was used
to load assemblies when your application was launched was reclaimed by
memory management when you minimized the application.
Did you improve memory management or application performance by minimizing and
maximizing your application? No. You can find some .Net Windows
programmers who add code to minimize and then maximize their programs
thinking that they are optimizing memory. As these programmers have
shared this technique with others a sort of .Net Urban Legend has been
born – a programming practice based on fiction not fact. This
practice is unnecessary because when OS needs the unused memory left
behind when the assemblies were loaded it will reclaim it
automatically. In fact, decreasing the size of your application’s
working set when memory is plentiful may decrease performance.
Instead of looking at the task manager you should use the PerfMon tool to see the memory consumption of your application. You will see the real memory checking this counter:
Process->VirtualBytes: Virtual Bytes is the current size, in bytes, of the virtual address space the process is using. Use of virtual address space does not necessarily imply corresponding use of either disk or main memory pages. Virtual space is finite, and the process can limit its ability to load libraries.
Garbage collection in .NET occurs when all your objects are dead or when you close the program. You can also call the garbage collector in .NET, but it's not generally practiced. And while in minimized mode the program, though working (or alive or whatever you can call), is not in focus or actively used by the user. So the memory use (system resources) that has been alloted to the program reduces so the other programs may use the system resources.
How are you determining the amount of memory your program uses?
I believe Windows Forms is known for calling SetProcessWorkingSetSize
to evict as much pages from RAM as possible.