Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Garbage Collection from K&B book

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me why the Object C3 is not eligible?
class CardBoard {
Short story = 5;
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
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.
Answer:
� 3 C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
�˚ A, B, D, E, and F are incorrect based on the above.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"GopiJava Ranch," please check your private messages by clicking on My Private Messages. Thanks!

In the above code, c3 is only assigned a null reference. c3 never references an actual object, so there is nothing to be garbage collected.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C3 is only a reference, it does'nt point to its own object for it to point to its own object the "new" command must be invoked.
Eg:


Only then does c3 point to an object.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, first of all tell me..What doyou mean by Object C3, We never have name for Objects, We can have only 3 things related with Object:
Type of Object, Reference to the Object, and Object Properties..(instance variable and methods),

So When we write following:

CardBoard c1 = new CardBoard();

we create one Object and
Type = CardBoard
C1 on stack referring to this Object
Properties : story(Short Object),go method..

So this line will create two Objects on the Heap..
One of type CardBoard and other of type Short...

Now investigate this line closely:

CardBoard c3 = c1.go(c2);

and tell how many Object will be created after this line, Does C3 refers to any Object or not?

If you can answer this question ..then you can answer your own Question...!!!
 
GopiKrishnan Chelliah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much...... I understood.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by GopiJava Ranch:
Thank you so much...... I understood.


Whoops, you missed this part...

Please check your private messages by clicking on My Private Messages. Thanks!
 
This one time, at bandcamp, I had relations with a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic