Garbage collection of an object created in an infinite loop

I have a very simple question.

I am writing a loop like this:

while(true) { MyTestClass myObject = new MyTestClass(); } 
  • When will an object be created in a loop, garbage collection?
  • Also, for each iteration, is that a new memory location allocated for the myObject reference?
  • What if I write myObject = null; at the end of each iteration?
+6
garbage-collection c #
source share
5 answers
  • whenever the GC feels that way, frankly; a variable is never read, so it always has the right
  • myObject is a variable that has a fixed stack position for reference; however, each new MyTestClass() is a different object created somewhere in the available heap space; every time every time
  • no difference; strictly speaking, there are some difficulties associated with the actual variable declaration point (in IL) and how while actually implemented, but this will only be displayed after exiting the loop. And since you immediately select it at each iteration, there is no tangible difference.
+9
source share

When will an object be created in a loop, garbage collected?

At some point after removing the last link to it. A new link is created and deleted at each iteration of the loop, so the GC can collect these objects whenever it wants. In practice, this can happen when your program completely fills the zero generation level.

In addition, each iteration requires a new memory location to be bound to myObject?

Yes.

What if I write myObject = null; at the end of each iteration?

It does not matter. Setting myObject = null; deletes this reference to the object, but the link is deleted anyway when the variable myObject reassigned in the next iteration of the loop.

+5
source share

Add another code that actually uses this object to make it more understandable:

 while(true) { // Here a new instance is created in each iteration: MyTestClass myObject = new MyTestClass(); // Here the instance is still in use // until here: myObject.CallSomething(); // Here the instance isn't used any more, // so the GC can collect it if it wants to. // Setting the reference to null here: myObject = null; // is useless, as the GC already knows that the // instance is unused before that time. } 
+4
source share

Let me figure it out a bit. Each time you get inside the loop, a new address will be allocated to myObject . Thus, this entire cycle always allocates new memory addresses for a single variable name. Thus:

  • The GC will garbage collect all previous distributions, perhaps almost immediately, since the variable is never used.
  • Yes.
  • No matter. The variable is still not used.
+3
source share

In addition to the rest of the answers:

You can make your class a structure. Then it will be on the stack and will be thrown for each iteration. If your structure creates new classes, you will return to the square. And if your structure is large, it can affect performance negatively, but otherwise, if it is small, it can affect performance positively.

0
source share

All Articles