• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collector vs circular reference

 
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I came across code at work, and I try to figured it out how the garbage collector will handle this case.
In fact, it looks like a goof tricky question for the exam !

Object A have an attribute Object B
Object B have an attribute Object A





objA will obviously be eligible for GC !
But what about objectB and objectA? They still reference each other.

I'm tempted to say that they will not be GC !!

What do you think?

Sylvain
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The garbage collector doesn't have a problem with identifying those objects as being available for garbage collection. The fact that you don't know how it does that doesn't prevent it from doing its work.
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

So you're saying All three objects (two references) will be GC ?
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only see two objects. There's an object of type A, and an object of type B, and they contain references to each other.

And no, I'm not saying those two objects will be garbage-collected. I'm saying they are available to be garbage-collected. Whether the garbage collector actually collects them depends on whether it's necessary to do so, and that's a decision which is up to the garbage collector.
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I've misused the word GC, I meant "candidate for GC" !

For the three object:
- objA in the main function
- objectB attribute in Class A
- objectA attribute in Class B, which is the same reference as the object in the main function

Three object, two references...

So, If i get back to my original question and your explanation: all objects will be candidate for GC.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you have that backwards. There are three variables which at some times contain references to one of the two objects. It's the objects which get garbage-collected, not the variables. So discussions which talk about "variable X being garbage-collected" are misleading, although you'll find they are extremely common in this forum.
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your point ! You're absolutely right.
I think in terms of variables, when I should think object "creation"

Object Reference
A ---> objA
---> objectA

B ---> objectB

--------------------------------------
2 Objects candidate to be GC
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think you have it now. Although when making a diagram like that, I would have the arrows (which represent references) going from the variables to the objects which they reference. You have the arrows backwards.

Note that it's possible for several variables to contain references to a single object, and in fact that is the case in this example for part of the time. However a variable can only refer to one object at a time, unless it contains null in which case it doesn't refer to any object.
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul, for your help!
I got the picture and will surely score higher in this objective !
reply
    Bookmark Topic Watch Topic
  • New Topic