/** * See FinalizationTester for discussion on how to best trigger GC in tests. * https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/ * java/lang/ref/FinalizationTester.java */ public static void awaitGarbageCollection() throws Exception { Runtime.getRuntime().gc(); Thread.sleep(100); System.runFinalization(); } }
/** * Runs the finalization methods of any objects pending finalization. * <p> * Calling this method suggests that the Java Virtual Machine expend * effort toward running the <code>finalize</code> methods of objects * that have been found to be discarded but whose <code>finalize</code> * methods have not yet been run. When control returns from the * method call, the Java Virtual Machine has made a best effort to * complete all outstanding finalizations. This method calls * Sys.runFinalization() to run finalization. */ public static void runFinalization() { java.lang.System.runFinalization(); } }
public static void runFinalization() { System.runFinalization(); }
public static void runFinalization() { System.runFinalization(); }
@Override public void runGc() { // Code taken from AOSP FinalizationTest: // https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/ // java/lang/ref/FinalizationTester.java // System.gc() does not garbage collect every time. Runtime.gc() is // more likely to perform a gc. Runtime.getRuntime().gc(); enqueueReferences(); System.runFinalization(); }
/** * This method exists for binary compatibility. It is equivalent * to {@link System#runFinalization}. */ @Deprecated public void runFinalizationSync() { System.runFinalization(); }
private static void collectGarbage() { try { System.gc(); Thread.sleep(fSLEEP_INTERVAL); System.runFinalization(); Thread.sleep(fSLEEP_INTERVAL); } catch (InterruptedException ex) { ex.printStackTrace(); } } }
private static void collectGarbage() { try { System.gc(); Thread.sleep(fSLEEP_INTERVAL); System.runFinalization(); Thread.sleep(fSLEEP_INTERVAL); } catch (InterruptedException ex) { ex.printStackTrace(); } } }
/** Also retries up to 500ms to improve GC race condition situation. */ private static boolean isFileOpen(String canonicalPath) { synchronized (openFiles) { int tries = 0; while (tries < 5 && openFiles.contains(canonicalPath)) { tries++; System.gc(); System.runFinalization(); System.gc(); System.runFinalization(); try { openFiles.wait(100); } catch (InterruptedException e) { // Ignore } } return openFiles.contains(canonicalPath); } }
/** * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero, * invoking the garbage collector as necessary to try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void await(CountDownLatch latch) { if (latch.getCount() == 0) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (latch.getCount() == 0) { return; } System.gc(); try { if (latch.await(1L, SECONDS)) { return; } } catch (InterruptedException ie) { throw new RuntimeException("Unexpected interrupt while waiting for latch", ie); } } while (System.nanoTime() - deadline < 0); throw formatRuntimeException( "Latch failed to count down within %d second timeout", timeoutSeconds); }
final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (future.isDone()) { return;
System.runFinalization();
public static void gc() { System.gc(); System.runFinalization(); try { Thread.sleep(1000); } catch (InterruptedException e) { // IGNORE } } }
@SuppressFBWarnings("DM_GC") static boolean tryRecursiveDelete(File file) { boolean interrupted = false; try { for (int i = 0; i < 5; i++) { if (recursiveDelete(file) || !isWindows()) { return true; } else { System.gc(); System.runFinalization(); try { Thread.sleep(50); } catch (InterruptedException e) { interrupted = true; } } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } return false; }
/** * Waits until the given predicate returns true, invoking the garbage collector as necessary to * try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void awaitDone(FinalizationPredicate predicate) { if (predicate.isDone()) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (predicate.isDone()) { return; } CountDownLatch done = new CountDownLatch(1); createUnreachableLatchFinalizer(done); await(done); if (predicate.isDone()) { return; } } while (System.nanoTime() - deadline < 0); throw formatRuntimeException( "Predicate did not become true within %d second timeout", timeoutSeconds); }
/** * Runs all JSR166 unit tests using junit.textui.TestRunner. * Optional command line arg provides the number of iterations to * repeat running the tests. */ public static void main(String[] args) { if (useSecurityManager) { System.err.println("Setting a permissive security manager"); Policy.setPolicy(permissivePolicy()); System.setSecurityManager(new SecurityManager()); } int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); Test s = suite(); for (int i = 0; i < iters; ++i) { junit.textui.TestRunner.run(s); System.gc(); System.runFinalization(); } System.exit(0); }
/** * Call GC until no more memory can be freed */ public static void restoreJvm() { int maxRestoreJvmLoops = 10; long memUsedPrev = memoryUsed(); for (int i = 0; i < maxRestoreJvmLoops; i++) { System.runFinalization(); System.gc(); long memUsedNow = memoryUsed(); // break early if have no more finalization and get constant mem used if ((ManagementFactory.getMemoryMXBean().getObjectPendingFinalizationCount() == 0) && (memUsedNow >= memUsedPrev)) { break; } else { memUsedPrev = memUsedNow; } } }
protected final void tearDown(SystemTestData testData) throws Exception { if (testData.isTestDataAvailable()) { onTearDown(testData); destroyGeoServer(); TestHttpClientProvider.endTest(); // some tests do need a kick on the GC to fully clean up if (isMemoryCleanRequired()) { System.gc(); System.runFinalization(); } } }
@After public void tearDown() throws Exception { // Collect dangling Cursors and TXs before store closes System.gc(); System.runFinalization(); if (store != null) { try { store.close(); store.deleteAllFiles(); File[] files = boxStoreDir.listFiles(); if (files != null) { for (File file : files) { logError("File was not deleted: " + file.getAbsolutePath()); } } } catch (Exception e) { logError("Could not clean up test", e); } } deleteAllFiles(); }
/** If subclasses overide they *must* call super.tearDown() first. */ @Override protected void oneTimeTearDown() throws Exception { if (getTestData().isTestDataAvailable()) { try { // dispose WFS XSD schema's - they will otherwise keep geoserver instance alive // forever!! disposeIfExists(getXSD11()); disposeIfExists(getXSD10()); // kill the context applicationContext.destroy(); // kill static caches GeoServerExtensionsHelper.init(null); // some tests do need a kick on the GC to fully clean up if (isMemoryCleanRequired()) { System.gc(); System.runFinalization(); } if (getTestData() != null) { getTestData().tearDown(); } } finally { applicationContext = null; testData = null; } } }