File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is system.gc() and finalize in the exam?

this is from mughals book....
 
atluri sandeep
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer to the code is c
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d ??
 
Jacob Sonia
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the code should be c i believe, coz all the 10 objects formed are eligible for GC
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In SCJP 6 the topic of using System.gc() has been removed from the exam!
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..


 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic