Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that in .NET the garbage collector gets called when you minimize the program?

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?

like image 988
Anirudha Avatar asked Feb 01 '11 13:02

Anirudha


People also ask

How does the .NET garbage collector work?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

What triggers garbage collection in C#?

Garbage Collection occurs if at least one of multiple conditions is satisfied. These conditions are given as follows: If the system has low physical memory, then garbage collection is necessary. If the memory allocated to various objects in the heap memory exceeds a pre-set threshold, then garbage collection occurs.

What does the garbage collector GC in .NET do Mcq?

Garbage collector is used for deallocate memory for unrefrenced objects and complete object.

Is garbage collection controlled by a program?

Explanation: Garbage Collection cannot be controlled by a program.


3 Answers

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).

like image 118
Oded Avatar answered Oct 18 '22 11:10

Oded


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.

like image 40
Cody Gray Avatar answered Oct 18 '22 13:10

Cody Gray


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.

  1. Create and run the simple application.

  2. 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.

  3. 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.

like image 41
Anirudha Avatar answered Oct 18 '22 11:10

Anirudha