Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular References in Java

Given an aggregation of class instances which refer to each other in a complex, circular, fashion: is it possible that the garbage collector may not be able to free these objects?

I vaguely recall this being an issue in the JVM in the past, but I thought this was resolved years ago. yet, some investigation in jhat has revealed a circular reference being the reason for a memory leak that I am now faced with.

Note: I have always been under the impression that the JVM was capable of resolving circular references and freeing such "islands of garbage" from memory. However, I am posing this question just to see if anyone has found any exceptions.

like image 223
Ryan Delucchi Avatar asked Oct 06 '08 23:10

Ryan Delucchi


People also ask

What are circular references in Java?

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

What is meant by circular reference?

A circular reference refers to a formula, that visits its own or another cell more than once in its chain of calculations, creating an infinite loop which slows down your spreadsheet significantly.

Does Java garbage collector handle cyclic references?

yes Java Garbage collector handles circular-reference!

What is wrong with circular references?

The circular reference error message "There are one or more circular references where a formula refers to its own cell either directly or indirectly. This might cause them to calculate incorrectly. Try removing or changing these references, or moving the formulas to different cells."


1 Answers

Only a very naive implementation would have a problem with circular references. Wikipedia has a good article on the different GC algorithms. If you really want to learn more, try (Amazon) Garbage Collection: Algorithms for Automatic Dynamic Memory Management . Java has had a good garbage collector since 1.2 and an exceptionally good one in 1.5 and Java 6.

The hard part for improving GC is reducing pauses and overhead, not basic things like circular reference.

like image 67
David G Avatar answered Sep 18 '22 22:09

David G