java.lang.Runtime.gc()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(239)

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

Runtime.gc介绍

[英]Indicates to the VM that it would be a good time to run the garbage collector. Note that this is a hint only. There is no guarantee that the garbage collector will actually be run.
[中]向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: 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. * Indicates to the VM that it would be a good time to run the
  3. * garbage collector. Note that this is a hint only. There is no guarantee
  4. * that the garbage collector will actually be run.
  5. */
  6. public static void gc() {
  7. Runtime.getRuntime().gc();
  8. }

代码示例来源:origin: cmusphinx/sphinx4

  1. public String execute(CommandInterpreter ci, String[] args) {
  2. Runtime.getRuntime().gc();
  3. return "";
  4. }

代码示例来源:origin: skylot/jadx

  1. @Override
  2. public void mouseClicked(MouseEvent e) {
  3. Runtime.getRuntime().gc();
  4. update();
  5. if (LOG.isDebugEnabled()) {
  6. LOG.debug("Memory used: {}", Utils.memoryInfo());
  7. }
  8. }
  9. });

代码示例来源:origin: Sable/soot

  1. public static long getFreeLiveMemory() {
  2. Runtime r = Runtime.getRuntime();
  3. r.gc();
  4. return r.freeMemory();
  5. }

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

  1. @Override
  2. @SuppressWarnings("CallToSystemGC")
  3. public void execute(Map<String, List<String>> parameters, PrintWriter output) {
  4. final int count = parseRuns(parameters);
  5. for (int i = 0; i < count; i++) {
  6. output.println("Running GC...");
  7. output.flush();
  8. runtime.gc();
  9. }
  10. output.println("Done!");
  11. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. private static void checkMemoryUsage() {
  2. Runtime runtime = Runtime.getRuntime();
  3. runtime.gc();
  4. long memory = runtime.totalMemory() - runtime.freeMemory();
  5. log.info("USED MEMORY (bytes): " + memory);
  6. }

代码示例来源:origin: com.h2database/h2

  1. private static synchronized void collectGarbage() {
  2. Runtime runtime = Runtime.getRuntime();
  3. long total = runtime.totalMemory();
  4. long time = System.nanoTime();
  5. if (lastGC + TimeUnit.MILLISECONDS.toNanos(GC_DELAY) < time) {
  6. for (int i = 0; i < MAX_GC; i++) {
  7. runtime.gc();
  8. long now = runtime.totalMemory();
  9. if (now == total) {
  10. lastGC = System.nanoTime();
  11. break;
  12. }
  13. total = now;
  14. }
  15. }
  16. }

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

  1. @SuppressWarnings("all")
  2. private long gc() {
  3. final long before = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  4. Runtime.getRuntime().gc();
  5. final long after = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  6. return (before - after) / 1024;
  7. }

代码示例来源:origin: lealone/Lealone

  1. private static synchronized void collectGarbage() {
  2. Runtime runtime = Runtime.getRuntime();
  3. long total = runtime.totalMemory();
  4. long time = System.currentTimeMillis();
  5. if (lastGC + GC_DELAY < time) {
  6. for (int i = 0; i < MAX_GC; i++) {
  7. runtime.gc();
  8. long now = runtime.totalMemory();
  9. if (now == total) {
  10. lastGC = System.currentTimeMillis();
  11. break;
  12. }
  13. total = now;
  14. }
  15. }
  16. }

代码示例来源:origin: osmandapp/Osmand

  1. protected static long runGCUsedMemory() {
  2. Runtime runtime = Runtime.getRuntime();
  3. long usedMem1 = runtime.totalMemory() - runtime.freeMemory();
  4. long usedMem2 = Long.MAX_VALUE;
  5. int cnt = 4;
  6. while (cnt-- >= 0) {
  7. for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++i) {
  8. // AVIAN FIXME
  9. runtime.runFinalization();
  10. runtime.gc();
  11. Thread.yield();
  12. usedMem2 = usedMem1;
  13. usedMem1 = runtime.totalMemory() - runtime.freeMemory();
  14. }
  15. }
  16. return usedMem1;
  17. }

代码示例来源:origin: com.h2database/h2

  1. /**
  2. * Call the garbage collection and run finalization. This close all files
  3. * that were not closed, and are no longer referenced.
  4. */
  5. static void freeMemoryAndFinalize() {
  6. IOUtils.trace("freeMemoryAndFinalize", null, null);
  7. Runtime rt = Runtime.getRuntime();
  8. long mem = rt.freeMemory();
  9. for (int i = 0; i < 16; i++) {
  10. rt.gc();
  11. long now = rt.freeMemory();
  12. rt.runFinalization();
  13. if (now == mem) {
  14. break;
  15. }
  16. mem = now;
  17. }
  18. }

代码示例来源:origin: apache/kylin

  1. public static int gcAndGetSystemAvailMB() {
  2. final int tolerance = 1;
  3. try {
  4. int lastMB = -1;
  5. while (true) {
  6. Runtime.getRuntime().gc();
  7. Thread.sleep(1000);
  8. int thisMB = getSystemAvailMB();
  9. if (lastMB < 0) {
  10. lastMB = thisMB;
  11. continue;
  12. }
  13. if (lastMB - thisMB < tolerance) {
  14. return thisMB;
  15. }
  16. lastMB = thisMB;
  17. }
  18. } catch (InterruptedException e) {
  19. Thread.currentThread().interrupt();
  20. logger.error("", e);
  21. return getSystemAvailMB();
  22. }
  23. }

代码示例来源:origin: lealone/Lealone

  1. /**
  2. * Call the garbage collection and run finalization. This close all files
  3. * that were not closed, and are no longer referenced.
  4. */
  5. static void freeMemoryAndFinalize() {
  6. IOUtils.trace("freeMemoryAndFinalize", null, null);
  7. Runtime rt = Runtime.getRuntime();
  8. long mem = rt.freeMemory();
  9. for (int i = 0; i < 16; i++) {
  10. rt.gc();
  11. long now = rt.freeMemory();
  12. rt.runFinalization();
  13. if (now == mem) {
  14. break;
  15. }
  16. mem = now;
  17. }
  18. }

代码示例来源:origin: lipangit/JiaoZiVideoPlayer

  1. public void onAutoCompletion() {
  2. Runtime.getRuntime().gc();
  3. Log.i(TAG, "onAutoCompletion " + " [" + this.hashCode() + "] ");
  4. onEvent(JZUserAction.ON_AUTO_COMPLETE);
  5. dismissVolumeDialog();
  6. dismissProgressDialog();
  7. dismissBrightnessDialog();
  8. onStateAutoComplete();
  9. if (currentScreen == SCREEN_WINDOW_FULLSCREEN || currentScreen == SCREEN_WINDOW_TINY) {
  10. backPress();
  11. }
  12. JZMediaManager.instance().releaseMediaPlayer();
  13. JZUtils.scanForActivity(getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  14. JZUtils.saveProgress(getContext(), jzDataSource.getCurrentUrl(), 0);
  15. }

代码示例来源:origin: apache/drill

  1. public String getMemString(boolean runGC) {
  2. if (runGC) {
  3. Runtime.getRuntime().gc();
  4. }
  5. long endDirect = manager.getMemDirect();
  6. long endHeap = manager.getMemHeap();
  7. long endNonHeap = manager.getMemNonHeap();
  8. return String.format("d: %s(%s), h: %s(%s), nh: %s(%s)", //
  9. DrillStringUtils.readable(endDirect - startDirect), DrillStringUtils.readable(endDirect), //
  10. DrillStringUtils.readable(endHeap - startHeap), DrillStringUtils.readable(endHeap), //
  11. DrillStringUtils.readable(endNonHeap - startNonHeap), DrillStringUtils.readable(endNonHeap) //
  12. );
  13. }
  14. }

代码示例来源:origin: bumptech/glide

  1. @Test
  2. public void submit_withPreviousButNoLongerReferencedIdenticalRequest_completesFromMemoryCache() {
  3. // We can't allow any mocks (RequestListner, Target etc) to reference this request or the test
  4. // will fail due to the transient strong reference to the request.
  5. concurrency.get(
  6. GlideApp.with(context)
  7. .load(ResourceIds.raw.canonical)
  8. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  9. .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
  10. // Force the collection of weak references now that the listener/request in the first load is no
  11. // longer referenced.
  12. Runtime.getRuntime().gc();
  13. concurrency.pokeMainThread();
  14. concurrency.get(
  15. GlideApp.with(context)
  16. .load(ResourceIds.raw.canonical)
  17. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  18. .listener(requestListener)
  19. .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
  20. verify(requestListener)
  21. .onResourceReady(
  22. anyDrawable(),
  23. any(),
  24. anyDrawableTarget(),
  25. eq(DataSource.MEMORY_CACHE),
  26. anyBoolean());
  27. }

代码示例来源:origin: bumptech/glide

  1. @Test
  2. public void submit_withPreviousButNoLongerReferencedIdenticalRequest_doesNotRecycleBitmap() {
  3. // We can't allow any mocks (RequestListener, Target etc) to reference this request or the test
  4. // will fail due to the transient strong reference to the request.
  5. Bitmap bitmap =
  6. concurrency.get(
  7. GlideApp.with(context)
  8. .asBitmap()
  9. .load(ResourceIds.raw.canonical)
  10. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  11. .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
  12. // Force the collection of weak references now that the listener/request in the first load is no
  13. // longer referenced.
  14. Runtime.getRuntime().gc();
  15. concurrency.pokeMainThread();
  16. FutureTarget<Bitmap> future = GlideApp.with(context)
  17. .asBitmap()
  18. .load(ResourceIds.raw.canonical)
  19. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  20. .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS);
  21. concurrency.get(future);
  22. Glide.with(context).clear(future);
  23. clearMemoryCacheOnMainThread();
  24. BitmapSubject.assertThat(bitmap).isNotRecycled();
  25. }

代码示例来源:origin: bumptech/glide

  1. Runtime.getRuntime().gc();
  2. concurrency.pokeMainThread();
  3. try {

相关文章