This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg Garbage Collection" Watch "Reg Garbage Collection" New topic
Author

Reg Garbage Collection

Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
This is one of the Questions from Marcus Green's mock exam.
please go through the code...

What will happen when you compile and run the following code?

The answer given is
Compilation and eventually multiple outputs of "finalizing a Box"

Is it because since the while condition is true, it goes on creating new objects of type Box, and this continues, and at some point of time as these objects have no references, they will be eligible for garbage collection, so the finalize method is executed on these objects.

Please correct me if i am wrong.

Thanks in advance.
[ October 19, 2004: Message edited by: Barry Gaunt ]
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
I agree with you. But I am not sure as to why it will not go into a infinite loop and hence run out of memory after some time. Even though there are so many objects which are eligible for garbage collection, they are not necessarily garbage collected as soon as the object gets created, right?

Any more thoughts?
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
... and hence run out of memory after some time. Even though there are so many objects which are eligible for garbage collection, they are not necessarily garbage collected as soon as the object gets created


Vishnu is correct.

All these objects are being created in an infinite loop. However they have no reference pointing to them, and so are ELGIBLE for garbage collection immediately after being created. (Elgible I say, not garbage collected).

The jvm certainly will perform actual garbage collection on unreferenced objects before memory becomes too low, and so in this example, memory doesn't get depleted in the infinite loop. Although unreferenced objects keep getting created, many of the previously created unreferenced objects get garbage collected (showing the output) as the loop progresses.

These unreference objects use up more and more memory and therefore give a pretty good incentive for the garbage collection to run.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
This question tricked me as well initially.

Key to the answer is that while you cannot force the GC to run through code (as is well established Runtime.gc() only suggests the GC run) it is guaranteed to run before the JVM runs out of memory.
Therefore it WILL run at intervals during the endless loop cleaning out all those unreferenced objects.

Maybe someone can voice in whether this is what to expect from the exam as it looks to me like the question wants to trick you into giving the wrong answer rather than test your knowledge.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg Garbage Collection
 
Similar Threads
Question from Examsimulator
question from marcus green
Is finalize() call guranteed?
When finalize() will be called?
need help