views:

512

answers:

5

Does setting the object to null make it MARKED for GC?

EDIT: A class has got multiple static methods. when using these methods in your program, what is the best way to ensure the objects are marked for GC after a certain point?

+1  A: 

Similar question here.

EDIT: Sorry I didn't realize the above link was for Java.

RandomNoob
That link is specifically for Java, this question is asking about C#/VB.NET
Daniel Schaffer
Is the behavior similar for c# /.net runtime as well?
RJ
+7  A: 

Methods aren't garbage collected at all - so it's not really clear what your question means.

Likewise you never set an object to null. You can make the value of a variable null, but that doesn't nothing to any object that the variable previously referred to. It just means that next time the garbage collector looks for live object, that variable won't contribute any object to the set of objects which must be kept alive at the end of the GC.

I suggest you read Jeffrey Richter's article on garbage collection for a bit more background, then ask any further specific questions when you've got to grips with the basics.

Jon Skeet
+1  A: 

objects are not marked for GC, They are marked (by the existence of a variable that references or points to them) to NOT be Garbage collected. When every variable or reference to an object in all running threads, and in all global static variables, and in all cpu registers, has been deleted, gone out of scope or been set to null, then the object is no longer accessible, and the GC will collect it.

Charles Bretana
+1  A: 

If you are asking about what happens to objects referenced by variables in static methods, then those objects become elligible for garbage collection when they are no longer in scope.

If you are talking about objects referenced by static fields then these will, in simple terms, not be collected until they the references are set to null.

The following example may illustrate this better:

class Example
{
    private static object field1 = new object();

    public static void SomeMethod()
    {
        object variable1 = new object();

        // ...
    }

    public static void Deref()
    {
        field1 = null;
    }
}

The object referenced by field1 will be created when the class is loaded and will remain rooted even as objects of class Example are created and destroyed. The only way to have that object garbage collected would be to call the Deref() method which will dereference it by setting the reference to null. (Actually, it is possible to unload classes by unloading an app domain but this is somewhat more advanced and not something you are likely to come across that often.)

Conversely, the static method SomeMethod() creates an object and references it by the variable variable1. This object is elligible for garbage collection as soon as it goes out of scope (at the end of the method). Pratically, it could be collected earlier if the remainder of the method does not reference it.

Paul Ruane
A: 

Think of static methods as class methods. They are available whether an object exists or not. They have no affect on garbage collection.

Cylon Cat