• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on Garbage collection

 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 given as C. Short wrapper along with c1 object. But i doubt why c2 and c3 arent garbage collected ? Is it because we are passing object reference to the method so it cant be changed further ?
 
Mihai Fonoage
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because when calling the go() method, you are passing a copy of the reference variable c2, not the c2 reference variable itself, nor the object that c2 points to. This is why the object that c2 refers to is not yet ready to be garbage collected. Furthermore, in the go() method you are not creating any new object, so c3 never actually pointed to an object. Basically, c1 (together with the Short object) would be GC.

Mihai Fonoage
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ansuman, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried doing a search on this forum? This exact question comes up every few days or so.

Henry
 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the Short object is elligible for gc ?
 
Mihai Fonoage
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it, how would you use the Short instance variable of c1, if c1 == null? (I believe this is called isolating a reference)

Mihai Fonoage
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramesh Sahu:
why the Short object is elligible for gc ?


It's not. The Short object is still in the cache (for autoboxing). There is an errata for this question, due to this reason.

Henry
 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


It's not. The Short object is still in the cache (for autoboxing). There is an errata for this question, due to this reason.

Henry


The answer is given as

"Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible."
 
Mihai Fonoage
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Henry (but you probably already new that ). I forgot about the -128 to 127 range for Short and Integer.

Mihai Fonoage
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, this means that object(s) that reside in the pool, e.g String pool or wrapper pool, will be eligible for GC ?
 
Mihai Fonoage
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Details answering your question can be found here.

Mihai Fonoage
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic