diff --git a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java index b502586ef..2d7daa05a 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java @@ -25,6 +25,8 @@ import net.openhft.chronicle.core.Maths; import net.openhft.chronicle.core.OS; import net.openhft.chronicle.hash.*; import net.openhft.chronicle.hash.impl.util.BuildVersion; +import net.openhft.chronicle.hash.impl.util.Cleaner; +import net.openhft.chronicle.hash.impl.util.CleanerUtils; import net.openhft.chronicle.hash.impl.util.jna.PosixMsync; import net.openhft.chronicle.hash.impl.util.jna.WindowsMsync; import net.openhft.chronicle.hash.serialization.DataAccess; @@ -38,7 +40,6 @@ import net.openhft.chronicle.wire.Marshallable; import net.openhft.chronicle.wire.WireIn; import net.openhft.chronicle.wire.WireOut; import org.jetbrains.annotations.NotNull; -import sun.misc.Cleaner; import java.io.Closeable; import java.io.File; @@ -465,7 +466,7 @@ public abstract class VanillaChronicleHash doClean(cleanerInstance); + } catch (IllegalAccessException | InvocationTargetException e) { + LOGGER.error("Unable to create cleaner", e); + throw new RuntimeException(e); + } + } + + private static void doClean(Object cleanerInstance) { + try { + CLEAN_METHOD.invoke(cleanerInstance); + } catch (IllegalAccessException | InvocationTargetException e) { + LOGGER.warn("Failed to clean buffer", e); + } + } +} diff --git a/src/test/java/net/openhft/chronicle/hash/impl/util/CleanerUtilsTest.java b/src/test/java/net/openhft/chronicle/hash/impl/util/CleanerUtilsTest.java new file mode 100644 index 000000000..67a7082c9 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/hash/impl/util/CleanerUtilsTest.java @@ -0,0 +1,23 @@ +package net.openhft.chronicle.hash.impl.util; + +import org.junit.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; + +public class CleanerUtilsTest { + + @Test + public void testClean() throws InterruptedException { + CountDownLatch latch = new CountDownLatch(1); + Object ob = new Object(); + Cleaner cleaner = CleanerUtils.createCleaner(ob, latch::countDown); + + cleaner.clean(); + + assertTrue(latch.await(1, TimeUnit.SECONDS)); + } + +} \ No newline at end of file