| Author |
Doubt regarding GC
|
Madhu Desai
Ranch Hand
Joined: Jun 14, 2009
Posts: 42
|
|
Hi all
Ref: K&B - Chapter 3 - Self Test - Question 11
Following is the problem with answer, followed by my graphical imagination.
When line 18 reached, how many objects will be eligible for GC?
Answers:
A: 0
B: 1
C: 2
D: 3
E: 4
F: 5
Solution: B is correct. It should clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta objects through the static variable a2.b1 - because it's static.
My Doubt:
1. Is this graphical imagination correct?
2. I thought, since a1, b1, b2 are 'null', object 'Beta 1' and object 'Alpha 1' are eligible for GC. Because a1=null, object 'Alpha 1' will become 'Island' object eligible for GC. So total of 2 objects (Beta 1 and Alpha 1) are eligible for GC.
But the answer from K&B says only 1 object is eligible for GC.
Will any body explain me how? and what is meant by the text in bold in solution part?
|
Thanks
Preparing for SCJP 6
|
 |
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
You've created quite a good picture! MSPaint?
It's good. Left column (references) is on the stack and right column is on the heap.
Object B1 is NOT eligible for GC because you still have reference to it through the static variable. Static variable is ONE to THE CLASS not to the instance. Even if you delete (null) all of the references you will have the reference to B1 object. STATIC IS NOT CONNECTED WITH INSTANCE! Am I right?
I think I've cleared this out.
|
 |
Madhu Desai
Ranch Hand
Joined: Jun 14, 2009
Posts: 42
|
|
Mason Storm wrote:You've created quite a good picture! MSPaint?
Static variable is ONE to THE CLASS not to the instance. Even if you delete (null) all of the references you will have the reference to B1 object. STATIC IS NOT CONNECTED WITH INSTANCE! Am I right?
Oh yes definitely MsPaint.
I almost understood, but one small confusion... where exactly will class's static variable be? - Stack or Heap?
|
 |
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
Object B1 is on the heap. But reference to this object should be also ont the heap.
But on the other hand: in K&B:
- object and instance variables are on the heap,
- local variables and methods are on the stack.
So what about static variable? Is it like instace variable in this case? I have to ask someone with wider knowledge than me.
Please, help
|
 |
Madhu Desai
Ranch Hand
Joined: Jun 14, 2009
Posts: 42
|
|
Thanks Mason Storm
I think this is correct representation...
Still...where exactly will class's static variable be? - Stack or Heap?
|
 |
Garg Sharad
Greenhorn
Joined: Jun 20, 2009
Posts: 15
|
|
Madhu Desai wrote:Thanks Mason Storm
I think this is correct representation...
Still...where exactly will class's static variable be? - Stack or Heap?
object of a class always created in HEAP.
|
Cheers,
Sharad Garg| SCJP6 95%
|
 |
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
|
Yes, but what about the static reference?
|
 |
 |
|
|
subject: Doubt regarding GC
|
|
|