SpiritBob
SpiritBob

Reputation: 2662

When GC.Collect() is called and frees up more than 3GB of space, is this necessarily a good thing?

I've read many questions on this network in regards to when one should use GC.Collect(), but none address this concern.

If we somehow manage to use it, as such as it frees a large number of memory, is this necessarily a good thing, rather than simply relying on the Garbage Collector to manage the memory?

Regardless of how idle my ASP.NET application stays, or how much time passes, or operations are done, the memory that gets freed when explicitly calling GC.Collect() is never addressed (only memory on top of it is, like it's some kind of a cache.)

One theory I have in mind, is that perhaps the reason those 3GB+ are not freed (but rather the application frees memory above that threshold) might be due to reusing parts of it, or maybe some other helpful optimization, at the cost of, of course, using that memory throughout the run-time of the application.

Could anyone share some insight on these concerns? I'm following best practices and disposing objects whenever given the chance. No unmanaged resource is left to be finalized - it's always disposed by me.

EDIT: The Garbage Collector used is the workstation one, not the server.

Upvotes: 1

Views: 179

Answers (1)

Artfunkel
Artfunkel

Reputation: 1852

The garbage collector's job is to simulate a machine with infinite memory. If your system has enough unallocated memory to continue functioning, why should the collector spend CPU cycles performing the hard work of identifying unreferenced objects? That's a performance cost with zero benefit.

If left alone, the collector will run and free those 3GB when the system starts to run low on memory. Until that point it is dormant.

Upvotes: 1

Related Questions