void () { AtomicInteger onlyAfterGCCounter = new AtomicInteger(); AtomicInteger alwaysCounter = new AtomicInteger(); LowMemoryWatcher onlyAfterGCWatcher = LowMemoryWatcher.register(onlyAfterGCCounter::incrementAndGet, LowMemoryWatcher.LowMemoryWatcherType.ONLY_AFTER_GC); LowMemoryWatcher alwaysWatcher = LowMemoryWatcher.register(alwaysCounter::incrementAndGet, LowMemoryWatcher.LowMemoryWatcherType.ALWAYS); try { LowMemoryWatcher.onLowMemorySignalReceived(false); assertEquals(1, alwaysCounter.get()); assertEquals(0, onlyAfterGCCounter.get()); LowMemoryWatcher.onLowMemorySignalReceived(true); assertEquals(2, alwaysCounter.get()); assertEquals(1, onlyAfterGCCounter.get()); } finally { alwaysWatcher.stop(); onlyAfterGCWatcher.stop(); } }