java.lang.System.runFinalization()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(288)

本文整理了Java中java.lang.System.runFinalization()方法的一些代码示例,展示了System.runFinalization()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.runFinalization()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:runFinalization

System.runFinalization介绍

[英]Provides a hint to the VM that it would be useful to attempt to perform any outstanding object finalization.
[中]向VM提供一个提示,提示尝试执行任何未完成的对象终结将非常有用。

代码示例

代码示例来源:origin: square/okhttp

  1. /**
  2. * See FinalizationTester for discussion on how to best trigger GC in tests.
  3. * https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/
  4. * java/lang/ref/FinalizationTester.java
  5. */
  6. public static void awaitGarbageCollection() throws Exception {
  7. Runtime.getRuntime().gc();
  8. Thread.sleep(100);
  9. System.runFinalization();
  10. }
  11. }

代码示例来源:origin: btraceio/btrace

  1. /**
  2. * Runs the finalization methods of any objects pending finalization.
  3. * <p>
  4. * Calling this method suggests that the Java Virtual Machine expend
  5. * effort toward running the <code>finalize</code> methods of objects
  6. * that have been found to be discarded but whose <code>finalize</code>
  7. * methods have not yet been run. When control returns from the
  8. * method call, the Java Virtual Machine has made a best effort to
  9. * complete all outstanding finalizations. This method calls
  10. * Sys.runFinalization() to run finalization.
  11. */
  12. public static void runFinalization() {
  13. java.lang.System.runFinalization();
  14. }
  15. }

代码示例来源:origin: fengjiachun/Jupiter

  1. public static void runFinalization() {
  2. System.runFinalization();
  3. }

代码示例来源:origin: fengjiachun/Jupiter

  1. public static void runFinalization() {
  2. System.runFinalization();
  3. }

代码示例来源:origin: square/leakcanary

  1. @Override public void runGc() {
  2. // Code taken from AOSP FinalizationTest:
  3. // https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/
  4. // java/lang/ref/FinalizationTester.java
  5. // System.gc() does not garbage collect every time. Runtime.gc() is
  6. // more likely to perform a gc.
  7. Runtime.getRuntime().gc();
  8. enqueueReferences();
  9. System.runFinalization();
  10. }

代码示例来源:origin: robovm/robovm

  1. /**
  2. * This method exists for binary compatibility. It is equivalent
  3. * to {@link System#runFinalization}.
  4. */
  5. @Deprecated
  6. public void runFinalizationSync() {
  7. System.runFinalization();
  8. }

代码示例来源:origin: thinkaurelius/titan

  1. private static void collectGarbage() {
  2. try {
  3. System.gc();
  4. Thread.sleep(fSLEEP_INTERVAL);
  5. System.runFinalization();
  6. Thread.sleep(fSLEEP_INTERVAL);
  7. } catch (InterruptedException ex) {
  8. ex.printStackTrace();
  9. }
  10. }
  11. }

代码示例来源:origin: JanusGraph/janusgraph

  1. private static void collectGarbage() {
  2. try {
  3. System.gc();
  4. Thread.sleep(fSLEEP_INTERVAL);
  5. System.runFinalization();
  6. Thread.sleep(fSLEEP_INTERVAL);
  7. } catch (InterruptedException ex) {
  8. ex.printStackTrace();
  9. }
  10. }
  11. }

代码示例来源:origin: objectbox/objectbox-java

  1. /** Also retries up to 500ms to improve GC race condition situation. */
  2. private static boolean isFileOpen(String canonicalPath) {
  3. synchronized (openFiles) {
  4. int tries = 0;
  5. while (tries < 5 && openFiles.contains(canonicalPath)) {
  6. tries++;
  7. System.gc();
  8. System.runFinalization();
  9. System.gc();
  10. System.runFinalization();
  11. try {
  12. openFiles.wait(100);
  13. } catch (InterruptedException e) {
  14. // Ignore
  15. }
  16. }
  17. return openFiles.contains(canonicalPath);
  18. }
  19. }

代码示例来源:origin: google/guava

  1. /**
  2. * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero,
  3. * invoking the garbage collector as necessary to try to ensure that this will happen.
  4. *
  5. * @throws RuntimeException if timed out or interrupted while waiting
  6. */
  7. public static void await(CountDownLatch latch) {
  8. if (latch.getCount() == 0) {
  9. return;
  10. }
  11. final long timeoutSeconds = timeoutSeconds();
  12. final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
  13. do {
  14. System.runFinalization();
  15. if (latch.getCount() == 0) {
  16. return;
  17. }
  18. System.gc();
  19. try {
  20. if (latch.await(1L, SECONDS)) {
  21. return;
  22. }
  23. } catch (InterruptedException ie) {
  24. throw new RuntimeException("Unexpected interrupt while waiting for latch", ie);
  25. }
  26. } while (System.nanoTime() - deadline < 0);
  27. throw formatRuntimeException(
  28. "Latch failed to count down within %d second timeout", timeoutSeconds);
  29. }

代码示例来源:origin: google/guava

  1. final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
  2. do {
  3. System.runFinalization();
  4. if (future.isDone()) {
  5. return;

代码示例来源:origin: google/guava

  1. System.runFinalization();

代码示例来源:origin: Netflix/eureka

  1. public static void gc() {
  2. System.gc();
  3. System.runFinalization();
  4. try {
  5. Thread.sleep(1000);
  6. } catch (InterruptedException e) {
  7. // IGNORE
  8. }
  9. }
  10. }

代码示例来源:origin: ehcache/ehcache3

  1. @SuppressFBWarnings("DM_GC")
  2. static boolean tryRecursiveDelete(File file) {
  3. boolean interrupted = false;
  4. try {
  5. for (int i = 0; i < 5; i++) {
  6. if (recursiveDelete(file) || !isWindows()) {
  7. return true;
  8. } else {
  9. System.gc();
  10. System.runFinalization();
  11. try {
  12. Thread.sleep(50);
  13. } catch (InterruptedException e) {
  14. interrupted = true;
  15. }
  16. }
  17. }
  18. } finally {
  19. if (interrupted) {
  20. Thread.currentThread().interrupt();
  21. }
  22. }
  23. return false;
  24. }

代码示例来源:origin: google/guava

  1. /**
  2. * Waits until the given predicate returns true, invoking the garbage collector as necessary to
  3. * try to ensure that this will happen.
  4. *
  5. * @throws RuntimeException if timed out or interrupted while waiting
  6. */
  7. public static void awaitDone(FinalizationPredicate predicate) {
  8. if (predicate.isDone()) {
  9. return;
  10. }
  11. final long timeoutSeconds = timeoutSeconds();
  12. final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
  13. do {
  14. System.runFinalization();
  15. if (predicate.isDone()) {
  16. return;
  17. }
  18. CountDownLatch done = new CountDownLatch(1);
  19. createUnreachableLatchFinalizer(done);
  20. await(done);
  21. if (predicate.isDone()) {
  22. return;
  23. }
  24. } while (System.nanoTime() - deadline < 0);
  25. throw formatRuntimeException(
  26. "Predicate did not become true within %d second timeout", timeoutSeconds);
  27. }

代码示例来源:origin: ben-manes/caffeine

  1. /**
  2. * Runs all JSR166 unit tests using junit.textui.TestRunner.
  3. * Optional command line arg provides the number of iterations to
  4. * repeat running the tests.
  5. */
  6. public static void main(String[] args) {
  7. if (useSecurityManager) {
  8. System.err.println("Setting a permissive security manager");
  9. Policy.setPolicy(permissivePolicy());
  10. System.setSecurityManager(new SecurityManager());
  11. }
  12. int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
  13. Test s = suite();
  14. for (int i = 0; i < iters; ++i) {
  15. junit.textui.TestRunner.run(s);
  16. System.gc();
  17. System.runFinalization();
  18. }
  19. System.exit(0);
  20. }

代码示例来源:origin: jtablesaw/tablesaw

  1. /**
  2. * Call GC until no more memory can be freed
  3. */
  4. public static void restoreJvm() {
  5. int maxRestoreJvmLoops = 10;
  6. long memUsedPrev = memoryUsed();
  7. for (int i = 0; i < maxRestoreJvmLoops; i++) {
  8. System.runFinalization();
  9. System.gc();
  10. long memUsedNow = memoryUsed();
  11. // break early if have no more finalization and get constant mem used
  12. if ((ManagementFactory.getMemoryMXBean().getObjectPendingFinalizationCount() == 0) && (memUsedNow
  13. >= memUsedPrev)) {
  14. break;
  15. } else {
  16. memUsedPrev = memUsedNow;
  17. }
  18. }
  19. }

代码示例来源:origin: geoserver/geoserver

  1. protected final void tearDown(SystemTestData testData) throws Exception {
  2. if (testData.isTestDataAvailable()) {
  3. onTearDown(testData);
  4. destroyGeoServer();
  5. TestHttpClientProvider.endTest();
  6. // some tests do need a kick on the GC to fully clean up
  7. if (isMemoryCleanRequired()) {
  8. System.gc();
  9. System.runFinalization();
  10. }
  11. }
  12. }

代码示例来源:origin: objectbox/objectbox-java

  1. @After
  2. public void tearDown() throws Exception {
  3. // Collect dangling Cursors and TXs before store closes
  4. System.gc();
  5. System.runFinalization();
  6. if (store != null) {
  7. try {
  8. store.close();
  9. store.deleteAllFiles();
  10. File[] files = boxStoreDir.listFiles();
  11. if (files != null) {
  12. for (File file : files) {
  13. logError("File was not deleted: " + file.getAbsolutePath());
  14. }
  15. }
  16. } catch (Exception e) {
  17. logError("Could not clean up test", e);
  18. }
  19. }
  20. deleteAllFiles();
  21. }

代码示例来源:origin: geoserver/geoserver

  1. /** If subclasses overide they *must* call super.tearDown() first. */
  2. @Override
  3. protected void oneTimeTearDown() throws Exception {
  4. if (getTestData().isTestDataAvailable()) {
  5. try {
  6. // dispose WFS XSD schema's - they will otherwise keep geoserver instance alive
  7. // forever!!
  8. disposeIfExists(getXSD11());
  9. disposeIfExists(getXSD10());
  10. // kill the context
  11. applicationContext.destroy();
  12. // kill static caches
  13. GeoServerExtensionsHelper.init(null);
  14. // some tests do need a kick on the GC to fully clean up
  15. if (isMemoryCleanRequired()) {
  16. System.gc();
  17. System.runFinalization();
  18. }
  19. if (getTestData() != null) {
  20. getTestData().tearDown();
  21. }
  22. } finally {
  23. applicationContext = null;
  24. testData = null;
  25. }
  26. }
  27. }

相关文章