0

I am new to Java Garbage Collection and wondering if following codes will cause memory leak in Java. Why or why not? Thanks.

class ListNode {
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
        next = null;
    }
}

public class Test() {

    static void tryCreateMemoryLeak() {
        ListNode l1, l2;
        for (int i = 0; i < 1000000; i++) {
            l1 = new ListNode(1);
            l2 = new ListNode(2);

            // create a circle here
            // will this circle be reclaimed? if do, when?
            l1.next = l2;
            l2.next = l1;
        }
    }

    public static void main(String[] args) {
        tryCreateMemoryLeak();
    }

}
Leopold
  • 76
  • 1
  • 4

5 Answers5

2

As the Java JVM's GC is a tracing and mark-and-sweep garbage collector, not a reference counter, it's impossible to cause a memory leak in Java purely through circular references.

The reason for this is that A will reference B, B will reference A, but no other objects will reference A or B, therefore marking both A and B (during the mark phase) eligible for garbage collection.

The JVM's GC marks all objects in the heap as collectable, traces from its roots, such as static variables, classloaders and such, marks all objects reachable by this trace as not-collectable and therefore would never reach the A or B instance you allocated. Therefore, the JVM's GC will detect that A and B are eligible for garbage collection and reclaim their memory.

David Xu
  • 5,555
  • 3
  • 28
  • 50
2

Try having a look at

http://www.itcsolutions.eu/2011/02/08/tutorial-java-9-garbage-collection-and-memory-leaks/

The expatiation is pretty neat. It might be of help

user2866507
  • 166
  • 2
  • 5
2

A memory leak is retaining a live reference (by mistake) of something that should not be referenced anymore.

You won't fool any modern garbage collectors by just creating a complicated web of references. Garbage collectors are (and have been for quite a number of years) mature enough to deal with circular references reliably.

As a rule of thumb: Memory leaks today are never caused by the garbage collector being unable to collect unreferenced stuff - there is always a (often well concealed) live reference if something isn't collected.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Actually, you can have memory leaks in ARC by creating circular references. It all depends which garbage collection algorithm the GC is using. – David Xu May 02 '14 at 18:35
  • @DavidXu If its fooled by circles, I wouldn't consider it *a modern GC*. Do you?. BTW the question is tagged "java" and circular dependencies are extremely abdundand in java, even in the JDK. So any GC not dealing with circular dependencies would be limited to a very small problem domain in java. – Durandal May 02 '14 at 18:37
0

The GC recognizes even circles. As soon as you do not longer reference this circle (for example by leaving the scope), the GC will eventually release the memory. Creating a memory leak in Java is...difficult. But not impossible (depending on the actual definition of memory leak).

nils
  • 1,362
  • 1
  • 8
  • 15
0

Circular references will not cause a memory leak. The objects will be subject to garbage collection when there are no more live references to any of the objects in the chain of circular references. When the actual garbage collection takes place is implementation dependent.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521