diff --git a/src/test/java/net/openhft/chronicle/map/MemoryLeaksTest.java b/src/test/java/net/openhft/chronicle/map/MemoryLeaksTest.java index a31a4d087..e75d2ca15 100644 --- a/src/test/java/net/openhft/chronicle/map/MemoryLeaksTest.java +++ b/src/test/java/net/openhft/chronicle/map/MemoryLeaksTest.java @@ -109,32 +109,33 @@ public class MemoryLeaksTest { } long nativeMemoryUsedBeforeMap = nativeMemoryUsed(); int serializersBeforeMap = serializerCount.get(); - ChronicleMap map = getMap(); - long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed(); - assertEquals(expectedNativeMemory, nativeMemoryUsed()); - tryCloseFromContext(map); - WeakReference> ref = new WeakReference<>(map); - Assert.assertNotNull(ref.get()); - map = null; + try (ChronicleMap map = getMap()) { + long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed(); + assertEquals(expectedNativeMemory, nativeMemoryUsed()); + tryCloseFromContext(map); + WeakReference> ref = new WeakReference<>(map); + Assert.assertNotNull(ref.get()); + // map = null; - // Wait until Map is collected by GC - while (ref.get() != null) { - System.gc(); - byte[] garbage = new byte[10_000_000]; - Thread.yield(); - } - // Wait until Cleaner is called and memory is returned to the system - for (int i = 0; i < 6_000; i++) { - if (nativeMemoryUsedBeforeMap == nativeMemoryUsed() && // (*) - serializerCount.get() == serializersBeforeMap) { - break; + // Wait until Map is collected by GC + while (ref.get() != null) { + System.gc(); + byte[] garbage = new byte[10_000_000]; + Thread.yield(); } - System.gc(); - byte[] garbage = new byte[10_000_000]; - Thread.sleep(10); + // Wait until Cleaner is called and memory is returned to the system + for (int i = 0; i < 6_000; i++) { + if (nativeMemoryUsedBeforeMap == nativeMemoryUsed() && // (*) + serializerCount.get() == serializersBeforeMap) { + break; + } + System.gc(); + byte[] garbage = new byte[10_000_000]; + Thread.sleep(10); + } + Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed()); + Assert.assertEquals(serializersBeforeMap, serializerCount.get()); } - Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed()); - Assert.assertEquals(serializersBeforeMap, serializerCount.get()); } private long nativeMemoryUsed() { @@ -151,37 +152,39 @@ public class MemoryLeaksTest { throws IOException, InterruptedException { long nativeMemoryUsedBeforeMap = nativeMemoryUsed(); int serializersBeforeMap = serializerCount.get(); - ChronicleMap map = getMap(); - // One serializer should be copied to the map's valueReader field, another is copied from - // the map's valueReader field to the context - Assert.assertTrue(serializerCount.get() >= serializersBeforeMap + 2); - Assert.assertNotEquals(0, map.offHeapMemoryUsed()); - try { - long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed(); - assertEquals(String.format( - "used before map: %d, used by map: %d, expected used: %d, actual used: %d", - nativeMemoryUsedBeforeMap, - map.offHeapMemoryUsed(), - expectedNativeMemory, nativeMemoryUsed()), - expectedNativeMemory, nativeMemoryUsed()); - } finally { - tryCloseFromContext(map); - map.close(); + try (ChronicleMap map = getMap()) { + // One serializer should be copied to the map's valueReader field, another is copied from + // the map's valueReader field to the context + Assert.assertTrue(serializerCount.get() >= serializersBeforeMap + 2); + Assert.assertNotEquals(0, map.offHeapMemoryUsed()); + try { + long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed(); + assertEquals(String.format( + "used before map: %d, used by map: %d, expected used: %d, actual used: %d", + nativeMemoryUsedBeforeMap, + map.offHeapMemoryUsed(), + expectedNativeMemory, nativeMemoryUsed()), + expectedNativeMemory, nativeMemoryUsed()); + } finally { + tryCloseFromContext(map); + map.close(); + } + + assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed()); + // Wait until chronicle map context (hence serializers) is collected by the GC + for (int i = 0; i < 6_000; i++) { + if (serializerCount.get() == serializersBeforeMap) + break; + System.gc(); + byte[] garbage = new byte[10_000_000]; + Thread.sleep(10); + } + Assert.assertTrue(serializerCount.get() == serializersBeforeMap); + // This assertion ensures GC doesn't reclaim the map before or during the loop iteration + // above, to ensure that we test that the direct memory and contexts are released because + // of the manual map.close(), despite the "leak" of the map object itself. + Assert.assertEquals(0, map.offHeapMemoryUsed()); } - assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed()); - // Wait until chronicle map context (hence serializers) is collected by the GC - for (int i = 0; i < 6_000; i++) { - if (serializerCount.get() == serializersBeforeMap) - break; - System.gc(); - byte[] garbage = new byte[10_000_000]; - Thread.sleep(10); - } - Assert.assertTrue(serializerCount.get() == serializersBeforeMap); - // This assertion ensures GC doesn't reclaim the map before or during the loop iteration - // above, to ensure that we test that the direct memory and contexts are released because - // of the manual map.close(), despite the "leak" of the map object itself. - Assert.assertEquals(0, map.offHeapMemoryUsed()); } private ChronicleMap getMap() throws IOException {