Yego
Yego

Reputation: 75

Memory leak in j2ee application involving circular references and arrays

I've read that the Java Garbage Collector is smart enough to deal with circular references (A->B and B->A. Or A->B, B->C and C->A). However, we are having a memory leak issue involving three of our classes, and I'm wondering if their mutual relationships have something to do with it. This is a simplified version of them:

public class A {
    List<B> bes;
}

public class B {
    A a;
    C c;
}

public class C {
    List<B> bes;
}

So, as you can see, this design not only creates a chain of relationships (A->B, B->C, C->B and B->A), but it also involves multiple instances of the class in the middle (B) in both sides.

It is also my understanding that this is not a problem for standard Java applications because all the objects are bound to a main thread of execution (public static void main(String [] args)). However, our program lives inside an application server (IBM Websphere), so there is no main method.

We know for sure that there is a memory leak, and that those three classes are involved (based on the results of a memory analysis of the heap), what I want is to know if it is possible that the combination of these conditions are causing the leak.

Note: It is worth noticing that the relationships are unidirectional. For example, an instance of C holds a reference to a list of Bs but those Bs are not pointing back to C, and the As to which they are pointing, don't have a reference back to B.

Upvotes: 0

Views: 60

Answers (1)

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

No, circular references don't cause issues in Java. The object will be valid for GC as long as it is not reachable from any GC root ( Thread, local variable, static variable, JNI reference). Thats what you need to trace, GC roots and see if they holding references forever.

You can use a profiler for that.

Upvotes: 1

Related Questions