Results 1 to 6 of 6
Thread: Garbage Collection Question
- 10-02-2009, 10:32 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
Garbage Collection Question
Given:
class CardBoard {
Short story = 200;
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: (C)
However, how to see that its 2 here?
Obviously, most ppl mistake it to be a 1 at line: c1 = null;
- 10-02-2009, 10:42 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
A CardBoard has a story which is another object.
- 10-02-2009, 10:55 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
I see...
So when an instance is created - all its variables are considered too...
- 10-02-2009, 11:05 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Variables that are objects (object references) are considered.
- 10-03-2009, 01:01 AM #5
I believe that it's two because c1 and c3 are both null.
Since we can't see the story class we can't be sure what is pointed to it.
Then again, I'm a beginner too.
Mr. Beans
- 10-03-2009, 10:08 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No. No instantiated object was ever created to assign to c3, and even if c2 had been assigned to c3 (as that method call implies, although it only returns null), there would still only have been one (actually 2 because of the "embedded" Short), but with two references refering to it, and it is not references that need to be garbagecollected.
Similar Threads
-
Java Garbage Collection and destructors
By riddhik84 in forum New To JavaReplies: 1Last Post: 09-30-2009, 09:48 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM -
How setting an Object to null help Garbage Collection?
By piyu.sha in forum Advanced JavaReplies: 3Last Post: 10-06-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks