File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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" Watch "garbage collection" New topic
Author

garbage collection

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
is system.gc() and finalize in the exam?

this is from mughals book....


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
atluri sandeep
Greenhorn

Joined: Apr 19, 2007
Posts: 1
the answer to the code is c


Sandeep Atluri
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

d ??


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 172
The answer to the code should be c i believe, coz all the 10 objects formed are eligible for GC
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
the book says the answer is d......maybe because of the system.gc()..but i dont think system.gc is in the exam
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

In SCJP 6 the topic of using System.gc() has been removed from the exam!


SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

System.gc has been removed from the exam, but questions about how many objects are eligible for GC will be on the exam. So the question without considering the System.gc call is valid. The answer should be c according to me...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Sorry, i'm wondering about, is there any Java function that could be executed to check how many objects in JVM?..
Perhaps, if there's, it's easier to know the correct answer..

Yes, i think the answer to the code is C, because in the second new Eligible();, refer to null reference..
Although in the code there's looping and it would execute new Eligible(); 10 times, but only 5 objects has the same reference, (so only one object would be created from this looping), and the 5 other objects has no reference..
these 10 objects is eligible for GC..



Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Sorry, i'm wondering about, is there any Java function that could be executed to check how many objects in JVM?..
Perhaps, if there's, it's easier to know the correct answer.

The question is not about how many objects are garbage collected, its about how many are eligible for GC. This can be figured out from the code easily. If you just want to check how many objects are garbage collected, add a finalize method to the class like this

Adding a finalize method is a way of seeing how many objects were garbage collected. But this doesn't prove how many objects are eligible for GC, actual number of objects eligible for GC might be more than the number of objects garbage collected...
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks Ankit Garg for your reply..

as what Raju Raju Champaklal said, the book says the answer is D //10 objects are eligible for GC..
in this case, the number of objects were garbage collected same with the number of objects are eligible for GC..
so.., what's the best way to know the number of objects are eligible for GC?..
is it only through our logic?..

Thanks..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Leonardo Carreira wrote:in this case, the number of objects were garbage collected same with the number of objects are eligible for GC..

How can you say that?? If you run my program, most of the times you'll get the "released" text 10 times, but its not necessary. Not all objects will be garbage collected every time. 10 objects are eligible for garbage collection, but how many of them will actually be garbage collected before the program ends (i.e. JVM shuts down) cannot be predicted.
what's the best way to know the number of objects are eligible for GC?..
is it only through our logic?..

As far as I know, that is the only way of finding the number of objects eligible for GC i.e. using logic...
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Ankit Garg wrote:
How can you say that?? If you run my program, most of the times you'll get the "released" text 10 times, but its not necessary. Not all objects will be garbage collected every time. 10 objects are eligible for garbage collection, but how many of them will actually be garbage collected before the program ends (i.e. JVM shuts down) cannot be predicted.


Okay, Thanks Ankit Garg..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection
 
Similar Threads
Garbage collection
Garbage Collection
garbage collection and finalize.
One object eligible for garbage collection?
Garbage Collection Eligibility