- Home /
Does Garbage Collector Collect variables Made Inside Scope?
Since my statement in this Question is wrong I'm asking here to learn more about that
here at last answer says
myMethodVariable is already marked for garbage collection when it Start() is completed.
but this script is a little bit different
so it would mean that variable is marked for garbage collection when SomeFunction() exits
as far as I still understand means Garbage collector still needs to find and collect 1000 variables
void Start(){
for (int i=0; i<1000; i++) {
SomeFunction();
}
}
void SomeFunction(){
float Variable = 4;
// some code to use variable
if (Variable == 4){
Variable = 5;
}
}
but as @robertbu says that Start() and function() are different things, ...
does that mean?
IF in Start() after variable exits it will be marked for collect
but if in Function() it will be freed on the spot?
Garbage collection only collects things that are allocated out of the heap. Your variables because they are by value, are declared on the stack.
I may have mislead you here. If a variable is declared on the heap (like strings are), then when there are no longer references to that string it is freed. If the variable is declared inside of a function, then usually it passes out of scope (and therefore no longer has any references) when you return from a function. It is possible to preserve references to that variable (assu$$anonymous$$g it is not a value type), by passing that reference to some other place in the code. As long as something is referencing the variable, the garbage collector will not reclaim the memory.
Answer by Hoeloe · Oct 26, 2013 at 09:11 PM
The variable will not be visible outside the scope of SomeFunction. To understand how the garbage collector works, you need to understand a little about the Stack and the Heap.
The Stack is a bit like the "working frame" of the program. It "stacks up" local data, and executes code. Value type variables are stored on the stack, and reference types store pointers into the Heap. When a function enters, it gets the local "frame" pushed onto the top of the Stack, and when it exits, all the local data is popped off again. This means that value-type variables are instantly deleted the moment the function exits scope, and pointers to reference types are also instantly deleted.
The Heap is a little more complicated. In short, it stores data anywhere it can, and the program relies on being told where that data is (that's where the pointers come in). The garbage collector only works on the Heap. It does nothing to the Stack, except use it in determining what is "garbage". In short, it determines that garbage is whatever cannot be reached from the Stack, so it follows all the pointers in the Stack, recursively following through, marking everything it reaches. Then it sweeps over the Heap, deleting everything that is not marked. This means that if there is no pointer to an object on the Heap that can be reached from the Stack, it is deleted at the next garbage collect.
Now, remember that the Stack stores the current pointers to objects in the Heap, and that it deletes these pointers when the function exits. So, if a local object is created, it puts the object in the Heap, but the only pointer to it exists in the function's stack frame. This means that when the function exits, the only reference to the object is deleted. This in turn means that the object will not be reached during the next "mark" phase of the garbage collector, and so will be deleted.
Hopefully, this has helped a little in understanding how the garbage collector operates.
EDIT:
I'm not sure I made this quite clear, but I want to point out that as long as there is a reference to an object that is still in scope, then it will not be garbage collected.
thanks you actually answered this Q and the other one as well
and I learned for the other Q even if I'll mark stuff $$anonymous$$eepAlive it'll still have to sweep through whole stack and heap to see what to keep
meaning performance will not be better
You can't boost performance like that. What you can do, though, is control when you run the garbage collector (using System.GC.Collect). By using this at sensible points (for example, between having loaded a scene and displaying it), you can reduce the work the garbage collector has to do the next time, while ensuring memory isn't wasted. It's much more difficult to get right, though.
Your answer
Follow this Question
Related Questions
why does my object reference go out of scope? 1 Answer
Class Scope Variable 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Increase difficulty Space Shooter 2 Answers