• 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

Question about Garbage Collection

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class CardBoard {
Short story = 200;// line A
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
}
}
I extracted this question from Kathye Sierra book chapter 3
in the given sample answer it is said wrapper object (in line A) is also become eligible for garbage collection when execution reach //dostuff
please some one explain how does this wrapper object become eligible for GC
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when c1 is assigned to a new object, the variable short is assigned to a new Short wrapper object ...therefore now c1 refers to a object(Cardboard) and this object refers to a new object(Short)....when is c1 is made null the object referred by c1 is lost.....and thus the wrapper object referred by the object is also lost(cannot be referenced) as this object(CardBoard is itself lost).......

i hope you could understand....if not what did not you understand....i will try my best
 
Sajith Eashan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much i understood what happened, if you don't mind can i extend my question

Does it mean, the objects created in side an object are also become eligible for GC once the parent object become eligible

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean parent? does it mean a superclass? when we create a subclass object no superclass object is created.....

was this your question?
 
Sajith Eashan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not the superclass if Object A creates Object B,C, and D once Object A become eligible does B,C and D become eligible too
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think yes.....once is object A is gone there would be no variable to access the other objects.....the variables would have died.....

 
Sajith Eashan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks now i understand much better
 
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
Hi,
I don't want to make new topic, so I post my question here.
Why c3 is not eligible for GC? Corrent me if I'm wrong, when i added to above code:

it get:

 
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
See my comments below...

Sajith Eashan wrote:class CardBoard {
Short story = 200;// line A
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard(); // creating 2 objects 1. CardBoard class type of object and 2. Short type of object(the instance variable of class). - line 1
CardBoard c2 = new CardBoard(); // here we also creating 2 objects 1. CardBoard class type of object and 2. Short type of object(the instance variable of class). - line 2
CardBoard c3 = c1.go(c2); // here we refer c3 to null simply, no object created. - line 3
c1 = null; // here we are making eligible line 1 objects for garbage collection, and the rest of object will be remain in memory.
// do Stuff
}
}
I extracted this question from Kathye Sierra book chapter 3
in the given sample answer it is said wrapper object (in line A) is also become eligible for garbage collection when execution reach //dostuff
please some one explain how does this wrapper object become eligible for GC



 
Minhaj Mehmood
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

Kamil Wojcik wrote:Hi,
Why c3 is not eligible for GC? Corrent me if I'm wrong, when i added to above code:



well c3 is already null, we haven't created an object for c3.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic