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();
}
}