• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

doubt on Garbage collection

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Details answering your question can be found here.

Mihai Fonoage
reply
    Bookmark Topic Watch Topic
  • New Topic