I have a very basic question.
I write a loop like this:
while(true)
{
MyTestClass myObject = new MyTestClass();
}
myObject = null;
at the end of every iteration?The finalize() method is called by Garbage Collector, not JVM. However, Garbage Collector is one of the modules of JVM. Object class finalize() method has an empty implementation.
Concept: Java garbage collection is the process of releasing unused memory. Sometimes some objects are no longer required by the program and when there is no reference to an object, then that object should be released. This process is known as garbage collection.
Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.
Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.
myObject
is the variable - that has a fixed position on the stack for the reference; however, each new MyTestClass()
is a different object, created somewhere in the available heap space; different each timewhile
is 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 hereIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With