• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection and finalize.

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know,even if we call System.gc() it is not guaranteed that it will run. Also we are not sure when the garbage collector will run and also jsut before the garbage collector runs the finalize method on the object is called and that too it can be called only once. Well i came across 2 questions in khalid mughal book for SCJP6

and


Question is which all objects are elligible for garbage collection when they reach 1. Can you please justify the answer given here? because i cannot.

I think first one should be 10 as the references are gone out of the code block

And the second one i think it should be 0 because p will have a reference to the list that is added.

like this i =0 p->next->null
i=1 p->next->next->null and so on.


i think it is a.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think first one is (b) 5 because object created at line 6 is not referred by any reference and 5 of them are created, while object created at line 5 is referred by reference obj. Second one, I think it is (b) 5 because each time you for is looping, it creates an object that is not referred on the next loop. So 5 Link objects are created and just one is referred by p. Also, when finalize method runs just before Garbage Collector runs (and you are right, it cannot be forced to execute) it executes and creates a non-referred "X" object which is eligible for gc too. Third one, I think it is (a) 0 too. This is just what I think it doesn't mean it is the right answer, I am studying for SCJP 6 too. Could you please post what correct answers are given by the book? Regards
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the book the answers are not given....But i think my answers are right. In the first 1 its is 10 after all because the objects its referred or not it doesn't matter. outside the for the object reference is out of scope and thus the object create becomes eligible for gc. and in the second the objects are created and attached as in a list so in 2nd it will be 0 objects for gc. And in the 3rd yes there are no objects for gc.

In the 2nd the confusion was whether the object created will be referenced. I guess it does. because of p = new Link(p) the inside p is the previous reference variable that was created.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d - Impossible to tell
The object ref at line 5 will be reassigned on each loop, so before it goes out of scope there will be 4 old objects on the heap.
The object created at line 6 has no reference, so is instantly eligible.
However, there is no way of knowing if gc has already run, when it ran and thus how many objects remain on the heap for collection.

a - "p" has reference to previous "p", which has reference to previous "p"...
As for "X", it will be referenced from the String Pool and is not eligible.

a - "array" is still in scope and it is an array of primitives, not wrapper objects
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
list p = null;

in the loop

p = new list(p) there in new list(p) which p will be used??? I think here p will be null. This null reference will be passed to the constructor.


 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the loop it will be the previous p. When i=0, "null" will be passed in. Then when i=1, the p from i=0 will be passed in. Then when i=2, the p from i=1 will be passed in. And so on.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so all objects of p will be connected. n the reference variable will p. SO none of them will be elligible for gc.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think so.
(I updated my first answer as well, I'd left a bit out)
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess i understand...I am little stressed out and in my brain all concepts are pretty mixed up..
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:d - Impossible to tell
The object ref at line 5 will be reassigned on each loop, so before it goes out of scope there will be 4 old objects on the heap.
The object created at line 6 has no reference, so is instantly eligible.
However, there is no way of knowing if gc has already run, when it ran and thus how many objects remain on the heap for collection.


OK, but assume that GC would run after System.gc(), how many objects would be eligible?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:OK, but assume that GC would run after System.gc(), how many objects would be eligible?


I assume you mean that no GC has run previously, so in that case, 10.

5 from "Eligible obj = new Eligible();" as "obJ" will be out of scope, and 5 from "new Eligible();" as there was no object reference.

if "obj" had still been in scope, then the answer would be 9.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These confusions of GC comes with automatic garbage collection that was introduced in java6. well we 1st one is really hard to say because the gc will run within the loop also. This thing can be verified by overriding the finalize method and print a line in the method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic