Manual call to GC.Collect() doesn't do anything

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
QScend
Posts: 8
Joined: Tue Mar 04, 2008 5:40 pm

Manual call to GC.Collect() doesn't do anything

Post by QScend » Mon Mar 10, 2008 7:50 pm

Rather than take snapshots to force GC to occur, I wrote a small windows app with a button and label. The handler is as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GC.Collect()
GC.WaitForPendingFinalizers()
lblMessage.Text = New System.Text.StringBuilder("Garbage collection completed ").Append(Now.ToString).Append(".").ToString
End Sub

However, running the app will update the label with the current time, but it doesn't look like GC is occurring. Real-time view in .NET MP shows the instances still in memory, but if I click the snapshot button in .NET MP, the GC appears to occur and all of the instances I expected to get collected did. I was under the impression that GC is a global thing, and you can call GC from a separate app and GC will take place. My test app seems to show otherwise. Is GC.Collect not global across the current machine? If it's not, how can I force a global GC such as .Net MP does, or is that not possible since it looks like .Net MP is loading the tested application on top of itself?

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Wed Mar 12, 2008 4:23 pm

Each .NET process has its own managed heaps and runs the garbage collector independent of all other .NET processes. Thus, calling GC.Collect in one process will only affect the process making the call.

.NET Memory Profiler is able to initiate a garbage collection in the profiled process by using a profiling library that gets loaded into the profiled process.

I don't know of a simple way of forcing a GC in an external process or doing a machine wide GC. It is possible to perform it by using advanced techniques such as injecting code into the process using CreateRemoteThread, but I don't see any real use for this. Why do you want to perform a system wide garbage collection?
Best regards,

Andreas Suurkuusk
SciTech Software AB

QScend
Posts: 8
Joined: Tue Mar 04, 2008 5:40 pm

Post by QScend » Wed Mar 12, 2008 4:51 pm

It wasn't so much performing a system-wide GC as much as having a simple app that would force GC on the app that we're currently testing, without building in a temporary way to GC that app while we test. Perhaps it would be useful to add a button to .NET MP that simply does a GC on the app being tested, without having to take a snapshot? Our current dev box isn't all that beefy so having to collect snapshots every time we want to force GC can take a bit of time.

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Thu Mar 13, 2008 9:10 pm

Thank you for the suggestion. I have added the suggestion as a feature request. In a future version of .NET Memory Profiler we will probably make it possible to initiate a GC from the profiler (without collecting a full heap snapshot).
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests