Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt regarding GC

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mason Storm

I think this is correct representation...



Still...where exactly will class's static variable be? - Stack or Heap?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Lukas Sieradzki
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but what about the static reference?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic