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?

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.