I'm aware that GC will clean objects on the heap when there's no longer any reference to them on the stack.
I wonder if there's a way to force GC to act before that. The scenario I have is something like this.
public void A(){ IList foo = bar(); //does work on foo's items foobar(); //more code before it goes out of scope } I wish I could free the memory used by the collection before calling foobar(). Is there a way to do that?
ps.: I know it's bad code, but it's a legacy code which I can do nothing about right now.
UPDATE: As pointed out by InBetween, "The GC is allowed to collect any unrechable object, but it's not a certainty it will do so". Unfortunately, I have a memory requirement from some users and the app must keep its memory usage within certain limits, otherwise I'd usually let GC do its work.
GC.Collect();manually, but there's no guarantuee it will work as intended.null. The actual collection is still there.GC.Collectbut you shouldn't be doing that unless you have a very good reason. Let the GC do its job, it's much better at it that any of us.