aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection output doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection output doubt" Watch "Garbage collection output doubt" New topic
Author

Garbage collection output doubt

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
This is a question about garbage collection. The output is 10,20,10. In the line which has an arrow, t1 is nulled before t2. so shldnt the output be something like 10,10,20 instead?
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
No, the order is not guaranted in gc.


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
Somewhere in SCJP it says that calling the System.gc() is just a recommendation to the JVM to clean up some garbage. So, just like threads, my guess is there is no gaurantee that the objects will be garbage collected at all, let alone in any predictable order.

Is this a question on a mock exam?

I get the second output if I do the following:
t1=null;
System.gc();
t2=null; //<-
System.gc();
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
12.6.2 Finalizer Invocations are Not Ordered
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
as said that the order of finalize method is not decided....
in the following code the result is same as 10,20,10 for all the iterations and it remains same for any number of runs of the program.
Shouldn't the answer get changed to 10,10,20 for even a single time ??


Sathish Nagappan
Ranch Hand

Joined: Nov 05, 2005
Posts: 76
Originally posted by Vaibhav Chauhan:
as said that the order of finalize method is not decided....
in the following code the result is same as 10,20,10 for all the iterations and it remains same for any number of runs of the program.
Shouldn't the answer get changed to 10,10,20 for even a single time ??


That again depends on the algorithm used for GC. Typical Mark and Sweep algorithms , use a generalized starting point (such as LRU or FIFO) or what ever. So it is not possible to say that the objects might get garbage collected in a particular order at all.


SCJP, SCWCD, SCBCD, IBM XML, IBM OOAD, SCEA 5, ITAC - Distinguished IT Architect.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
The order may depend on order of creating of objects, since finalizable object may be registered somewhere in JVM internally when they are created. Sun JVM uses the same mechanism for this as for soft/weak/phantom references
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection output doubt