Should you use = null to free memory for variables declared in Start()?

If you only use a variable in Start(), does it help with garbage collection if you declare it to be null before the end of Start()?

I suppose this question applies to any function that will be called only once, but I assume Start() is the most common instance of that kind of function.

I believe mono's garbage collection works by monitoring references to objects and identifying isolated items - that is, items or groups of items which can no longer be referenced by the currently running program.

In this case, whether or not you null the reference before Start completes, the fact that the variable falls out of scope when Start() ends, means that it will be picked up promptly by the garbage collector.

Bear in mind that this only applies to non unity objects - Unity has its own way of dealing with objects which derive from its Object class, which seems to be entirely separate from Mono's garbage collection!

The important thing here - which is a bit unclear in the question - is where you actually declare the variable. It doesn't matter so much where you use it - what matters is where you declare it. The scope of the variable depends on where it is declared, and what the garbage collector does with it depends on the scope.

So, if you have:

public class MyCoolClass : MonoBehavior {

    private string myMemberVariable; // variable declaration with "instance scope"

    public void Start() {
        string myMethodVariable = "SomeCoolString"; // variable declaration and assignment with "method scope"
        myMemberVariable = "Another cool string!"; // assignment

        /* some funky code that actually makes use of these useless strings, like: */

        myMethodVariable = myMemberVariable; // using myMemberVariable to assign its value to myMethodVariable
    }
}

What happens is that myMethodVariable will be garbage collected at some point in time after Start() was executed. The reason is that it was declared in the method, and so its scope is in the method, the garbage collector will have it marked as "not in use" and drop it from memory (at some point in time).

However, myMemberVariable still keeps the value that was assigned to it in the Start() method. Thus, it will only be garbage collected, when your instance of MyCoolClass is "out of scope" (which happens only when MyCoolClass is destroyed, or when the game object it is attached to is destroyed).

So, in that case, "nulling" myMemberVariable at the end of Start() would make sense in regards to garbage collection. The question, of course, would be why you declare a variable that you only use in one method with instance scope in first place - but you may have your reasons (it seems that in the context of iPhone development, people use "pools of objects" in order to avoid garbage collection - and for such a "pool of object" this exact pattern does seem to make sense to me; and in that case, you probably would "null it" to make sure you don't add even the slightest amount of extra memory) ;-)

Duck is right. Mono currently uses the Boehm garbage collector, which is a mark and sweep collector. Once your variable goes out of scope, it will lose the reference to the heap allocated object in exactly the same way that it would if you set it to null. In any case, that object isn't going to be freed until the next time the GC stops the world.