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

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

本文整理了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

/**
  * 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();
 }
}

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

@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();
}

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

/**
 * 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.
 */
public static void gc() {
  Runtime.getRuntime().gc();
}

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

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

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

@Override
  public void mouseClicked(MouseEvent e) {
    Runtime.getRuntime().gc();
    update();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Memory used: {}", Utils.memoryInfo());
    }
  }
});

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

public static long getFreeLiveMemory() {
 Runtime r = Runtime.getRuntime();
 r.gc();
 return r.freeMemory();
}

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

@Override
@SuppressWarnings("CallToSystemGC")
public void execute(Map<String, List<String>> parameters, PrintWriter output) {
  final int count = parseRuns(parameters);
  for (int i = 0; i < count; i++) {
    output.println("Running GC...");
    output.flush();
    runtime.gc();
  }
  output.println("Done!");
}

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

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

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

private static synchronized void collectGarbage() {
  Runtime runtime = Runtime.getRuntime();
  long total = runtime.totalMemory();
  long time = System.nanoTime();
  if (lastGC + TimeUnit.MILLISECONDS.toNanos(GC_DELAY) < time) {
    for (int i = 0; i < MAX_GC; i++) {
      runtime.gc();
      long now = runtime.totalMemory();
      if (now == total) {
        lastGC = System.nanoTime();
        break;
      }
      total = now;
    }
  }
}

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

@SuppressWarnings("all")
private long gc() {
  final long before = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  Runtime.getRuntime().gc();
  final long after = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  return (before - after) / 1024;
}

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

private static synchronized void collectGarbage() {
  Runtime runtime = Runtime.getRuntime();
  long total = runtime.totalMemory();
  long time = System.currentTimeMillis();
  if (lastGC + GC_DELAY < time) {
    for (int i = 0; i < MAX_GC; i++) {
      runtime.gc();
      long now = runtime.totalMemory();
      if (now == total) {
        lastGC = System.currentTimeMillis();
        break;
      }
      total = now;
    }
  }
}

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

protected static long runGCUsedMemory()  {
  Runtime runtime = Runtime.getRuntime();
  long usedMem1 = runtime.totalMemory() - runtime.freeMemory();
  long usedMem2 = Long.MAX_VALUE;
  int cnt = 4;
  while (cnt-- >= 0) {
    for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++i) {
      // AVIAN FIXME
      runtime.runFinalization();
      runtime.gc();
      Thread.yield();
      usedMem2 = usedMem1;
      usedMem1 = runtime.totalMemory() - runtime.freeMemory();
    }
  }
  return usedMem1;
}

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

/**
 * Call the garbage collection and run finalization. This close all files
 * that were not closed, and are no longer referenced.
 */
static void freeMemoryAndFinalize() {
  IOUtils.trace("freeMemoryAndFinalize", null, null);
  Runtime rt = Runtime.getRuntime();
  long mem = rt.freeMemory();
  for (int i = 0; i < 16; i++) {
    rt.gc();
    long now = rt.freeMemory();
    rt.runFinalization();
    if (now == mem) {
      break;
    }
    mem = now;
  }
}

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

public static int gcAndGetSystemAvailMB() {
  final int tolerance = 1;
  try {
    int lastMB = -1;
    while (true) {
      Runtime.getRuntime().gc();
      Thread.sleep(1000);
      int thisMB = getSystemAvailMB();
      if (lastMB < 0) {
        lastMB = thisMB;
        continue;
      }
      if (lastMB - thisMB < tolerance) {
        return thisMB;
      }
      lastMB = thisMB;
    }
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    logger.error("", e);
    return getSystemAvailMB();
  }
}

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

/**
 * Call the garbage collection and run finalization. This close all files
 * that were not closed, and are no longer referenced.
 */
static void freeMemoryAndFinalize() {
  IOUtils.trace("freeMemoryAndFinalize", null, null);
  Runtime rt = Runtime.getRuntime();
  long mem = rt.freeMemory();
  for (int i = 0; i < 16; i++) {
    rt.gc();
    long now = rt.freeMemory();
    rt.runFinalization();
    if (now == mem) {
      break;
    }
    mem = now;
  }
}

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

public void onAutoCompletion() {
  Runtime.getRuntime().gc();
  Log.i(TAG, "onAutoCompletion " + " [" + this.hashCode() + "] ");
  onEvent(JZUserAction.ON_AUTO_COMPLETE);
  dismissVolumeDialog();
  dismissProgressDialog();
  dismissBrightnessDialog();
  onStateAutoComplete();
  if (currentScreen == SCREEN_WINDOW_FULLSCREEN || currentScreen == SCREEN_WINDOW_TINY) {
    backPress();
  }
  JZMediaManager.instance().releaseMediaPlayer();
  JZUtils.scanForActivity(getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  JZUtils.saveProgress(getContext(), jzDataSource.getCurrentUrl(), 0);
}

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

public String getMemString(boolean runGC) {
  if (runGC) {
   Runtime.getRuntime().gc();
  }
  long endDirect = manager.getMemDirect();
  long endHeap = manager.getMemHeap();
  long endNonHeap = manager.getMemNonHeap();
  return String.format("d: %s(%s), h: %s(%s), nh: %s(%s)", //
    DrillStringUtils.readable(endDirect - startDirect), DrillStringUtils.readable(endDirect), //
    DrillStringUtils.readable(endHeap - startHeap), DrillStringUtils.readable(endHeap), //
    DrillStringUtils.readable(endNonHeap - startNonHeap), DrillStringUtils.readable(endNonHeap) //
   );
 }
}

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

@Test
public void submit_withPreviousButNoLongerReferencedIdenticalRequest_completesFromMemoryCache() {
 // We can't allow any mocks (RequestListner, Target etc) to reference this request or the test
 // will fail due to the transient strong reference to the request.
 concurrency.get(
   GlideApp.with(context)
     .load(ResourceIds.raw.canonical)
     .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
     .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
 // Force the collection of weak references now that the listener/request in the first load is no
 // longer referenced.
 Runtime.getRuntime().gc();
 concurrency.pokeMainThread();
 concurrency.get(
   GlideApp.with(context)
     .load(ResourceIds.raw.canonical)
     .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
     .listener(requestListener)
     .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
 verify(requestListener)
   .onResourceReady(
     anyDrawable(),
     any(),
     anyDrawableTarget(),
     eq(DataSource.MEMORY_CACHE),
     anyBoolean());
}

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

@Test
public void submit_withPreviousButNoLongerReferencedIdenticalRequest_doesNotRecycleBitmap() {
 // We can't allow any mocks (RequestListener, Target etc) to reference this request or the test
 // will fail due to the transient strong reference to the request.
 Bitmap bitmap =
   concurrency.get(
     GlideApp.with(context)
       .asBitmap()
       .load(ResourceIds.raw.canonical)
       .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
       .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS));
 // Force the collection of weak references now that the listener/request in the first load is no
 // longer referenced.
 Runtime.getRuntime().gc();
 concurrency.pokeMainThread();
 FutureTarget<Bitmap> future = GlideApp.with(context)
   .asBitmap()
   .load(ResourceIds.raw.canonical)
   .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
   .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS);
 concurrency.get(future);
 Glide.with(context).clear(future);
 clearMemoryCacheOnMainThread();
 BitmapSubject.assertThat(bitmap).isNotRecycled();
}

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

Runtime.getRuntime().gc();
concurrency.pokeMainThread();
try {

相关文章