tags:

views:

44

answers:

3

I know that the static objects in .Net managed world are loaded in Loader Heap which is never going to be garbage collected.

What happens to the instance reference parameters passed to a static methods. Are they get garbage collected once the static function executed completely Or they are going to live forever as those instance reference variables are once passed to static method?

I am really confused this evening; Please guide me.

Thanks and regards

123Developer.

+3  A: 

Yes, they are garbage collected after the static function is executed. You are creating objects in a non-static context, therefore they are not created in the loader heap. Just passing the reference to a static function doesnt change anything at all.

Philip Daubmeier
+1  A: 

Once the static method is done with the variable, it doesn't hold a reference to it anymore (if you assign it to a static variable, that's different). That does not mean that it will be garbage collected immediately though. Unless you specifically tell the GC to run, when it does so is based on when the runtime feels it needs to free up memory. Truthfully, this might not be ever during the life of the application (the resources would be freed with the application ends). This however is highly unlikely, although technically possible.

Kevin
Correct - as long as one does not assign the instance variable to a static field or store a reference to it in one.
saret
+2  A: 

That you pass a reference to a static method does in no way imply that the object will be rooted in any way, so that alone is not enough to prevent it from being collected.

Whether or not it can be collected during the method call, or only after it has completed, depends on what the method does with the argument, and whether the object is in use outside of the method call.

For instance, consider this hypothetical code:

public static class Program()
{
    public static void Main()
    {
        SomeObject o = new SomeObject();
        OtherMethod(o);
    }

    private static void OtherMethod(SomeObject x)
    {
        // lots of code here, but none that uses x
    }
}

in this case, o can be collected during the call to OtherMethod, provided you compile and run a release build. For release builds, variables and parameters that are no longer in use, that is, there is no code that uses it, are considered dead, and thus no longer counted when checking if there are live references to the object.

In Debug build, all variables and parameters are artificially kept alive until methods return, so that you can set a breakpoint and inspect the variable, even though there is no code left that uses it.

So if you were to run the above code from a debug build, then the object would be kept alive until Main returned.

Lasse V. Karlsen
+1 very interesting additional info. didnt know that. Lesson I learned again: debug and release builds behave very differently under the hood, while it looks identical from the outside.
Philip Daubmeier
Cant give you the promised upvote, due to daily vote limit :(
Philip Daubmeier
+1 Must people do not realize this. And to be incredibly padantic there is nothing in the specification that would prohibit the CLR from making the instance eligible for GC even before the variable 'o' is assigned since its members are never dereferenced. In realiity the CLR probably does not have heuristics that are that sophisticated yet, but it is theoretically possible.
Brian Gideon