/** * 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(); } }
@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(); }
/** * 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(); }
public String execute(CommandInterpreter ci, String[] args) { Runtime.getRuntime().gc(); return ""; }
@Override public void mouseClicked(MouseEvent e) { Runtime.getRuntime().gc(); update(); if (LOG.isDebugEnabled()) { LOG.debug("Memory used: {}", Utils.memoryInfo()); } } });
public static long getFreeLiveMemory() { Runtime r = Runtime.getRuntime(); r.gc(); return r.freeMemory(); }
@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!"); }
private static void checkMemoryUsage() { Runtime runtime = Runtime.getRuntime(); runtime.gc(); long memory = runtime.totalMemory() - runtime.freeMemory(); log.info("USED MEMORY (bytes): " + memory); }
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; } } }
@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; }
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; } } }
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; }
/** * 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; } }
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(); } }
/** * 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; } }
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); }
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) // ); } }
@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()); }
@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(); }
Runtime.getRuntime().gc(); concurrency.pokeMainThread(); try {