• Post Reply Bookmark Topic Watch Topic
  • New Topic

Circular Reference in Java  RSS feed

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I was trying to understand "Circular references" which says one object has a reference to another and the second object has a reference to the first
but i am not able to understand this.

Then there is a code which says it has circular reference but i think it doesn't (or may be i do not understand). The code is



I tried drawing a figure which is below
Now after only object 1 is available for GC, but i do not see any circular reference.
Thanks



Filename: untitled.bmp
Description: Figure as per code
File size: 788 Kbytes
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code does not show a circular reference. By the way, have you tried compiling and running it? It will never reach line 24, and it will crash after a while in line 23. That's because the following happens:

  • Line 23: You create a new a
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Etcetera until the stack overflows

  • A circular reference happens when one object refers to another, and that other one refers to the first object. For example:

    Java's garbage collector is smart enough to clean up the objects if there are circular references, but there are no live threads that have any references to the objects anymore. So having a circular reference like this does not create a memory leak.
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jasper !

    As per your code, firstly in the main() method you have created two references variable "one" and "two" which are pointing to the object of "A" and "B" respectively.

    Then you have invoked the method setB() and you are passing the reference variable "two" which is pointing to Object of class B

    to the reference variable "b" which is also of type B (class B)

    So "two" and "b" both are pointing to Class B object

    so how come there is a circular reference.?

    Same is done with the reference variable "one"

     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rameshwar Soni wrote:So "two" and "b" both are pointing to Class B object

    so how come there is a circular reference.?

    Because that's what a circular reference is! You have a circular reference if you can keep following references and end up back where you started.

    In Jesper's example: start at one. one.b is the B object. one.b.a is the same A object you started with (so one.b.a == one). That's circular.

    It can be more complicated that that - you can have more than two classes involved. If you could arrange a bunch of classes and objects so that one.b.c.d.e.f.g.a == one, then you'd have a circular reference.
     
    Wendy L Gibbons
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so a circular reference is about variables being circular... not classes being circular.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!