• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

garbage collection

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is system.gc() and finalize in the exam?

this is from mughals book....
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer to the code is c
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d ??
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 78
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In SCJP 6 the topic of using System.gc() has been removed from the exam!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
reply
    Bookmark Topic Watch Topic
  • New Topic