I have a very basic question.
I write a loop like this:
while(true)
{
MyTestClass myObject = new MyTestClass();
}
- When will be the object created in
the loop, garbage collected? - Also, for every iteration, is it
that the new memory location is
allocated to the myObject reference? - What if i write
myObject = null;at the end of every iteration?
myObjectis the variable – that has a fixed position on the stack for the reference; however, eachnew MyTestClass()is a different object, created somewhere in the available heap space; different each timewhileis actually implemented – but that would only show after exiting the loop. And since on each iteration you immediately allocate it, there is no tangible difference here