• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on gc

 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are avialble for GC

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
}
}

i think 2 cause c1 is null and c3 is being returned as null so remaining two objects are C2 and short but K&B books says following


A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.

I mean answer is right but object is different why C1

[ September 30, 2008: Message edited by: mukki pandey ]
[ September 30, 2008: Message edited by: mukki pandey ]
 
Ali Khalfan
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the question here?
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant understand your question.
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are avialble for GC

according to me C2 and short but k&b says C1 and short how C1 is avialable for GC since its already assigned to null
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone reply please
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects created in the example are 4 i.e. one referenced by c1 and short in c1 & the other one by c2 and short in c2.
c3 is just a refernce variable having null value.
As c1 is assigned null value, only the object which is referenced by c1 and short in c1 are eligible for GC. c2 is still referring to the object which was created in line 2. So it is not eligiible for GC.

Remember copy of variable c2 is passed to the method c2!

one thing : you have missed closing brace for the method go()!
[ September 30, 2008: Message edited by: M SRILATHA ]
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check on the Errata for the book. The Short is created by autoboxing, and based on the value (and due to the cache), it isn't eligible for GC. In the corrected version, the short has a larger value.

Henry
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks :-) i got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic