Skip to content

Commit dda2343

Browse files
committed
Fix #192: AbstractTripleSort has memory leak.
AbstractTriplesSort registers with the JVM to be informed when the available memory gets full. However, it never unregistered from the JVM so that the JVM kept referring the instances effectively producing a memory leak. This commit fixes this leak by unregistering the module from the JVM when calling closeStream().
1 parent 162c1f1 commit dda2343

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/org/culturegraph/mf/stream/pipe/sort/AbstractTripleSort.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public int compare(final SortedTripleFileFacade o1, final SortedTripleFileFacade
173173
}
174174
}
175175
}
176+
MemoryWarningSystem.removeListener(this);
176177
}
177178

178179
protected void onFinished() {

src/test/java/org/culturegraph/mf/stream/pipe/sort/AbstractTripleSortTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package org.culturegraph.mf.stream.pipe.sort;
1717

18+
import static org.junit.Assert.assertTrue;
19+
20+
import java.lang.ref.ReferenceQueue;
21+
import java.lang.ref.WeakReference;
22+
1823
import org.culturegraph.mf.types.Triple;
1924
import org.junit.Before;
2025
import org.junit.Test;
@@ -49,4 +54,24 @@ public void shouldNotFailIfFlushingBeforeFirstRecord() {
4954
tripleSort.closeStream();
5055
}
5156

57+
/**
58+
* This test case may throw fail unexpectedly as it relies on the
59+
* garbage collector to run when calling {@code System.gc()}. This
60+
* is not guaranteed by the JVM.
61+
*/
62+
@Test
63+
public void issue192ShouldUnregisterFromTheJVMToNotCauseMemoryLeak() {
64+
65+
// Get weak reference for checking whether the object was actually freed later:
66+
final ReferenceQueue<AbstractTripleSort> refQueue = new ReferenceQueue<AbstractTripleSort>();
67+
final WeakReference<AbstractTripleSort> weakRef = new WeakReference<AbstractTripleSort>(tripleSort, refQueue);
68+
69+
tripleSort.closeStream();
70+
tripleSort = null;
71+
72+
System.gc();
73+
74+
assertTrue(weakRef.isEnqueued());
75+
}
76+
5277
}

0 commit comments

Comments
 (0)