Skip to content
Permalink
Browse files

Add timeout to GCUtil.

It really is just best effort to call System.gc, so we can't make this
work in a generic way, without a JNI integration perhaps.
  • Loading branch information
LadyCailin committed Nov 5, 2019
1 parent 7cd37d0 commit 01e37d42deaabcb2e262cf01b1045557bc0dce3d
Showing with 27 additions and 1 deletion.
  1. +27 −1 src/main/java/com/laytonsmith/PureUtilities/GCUtil.java
@@ -34,9 +34,31 @@ public static void main(String[] args) {
* <p>
* If -XX:+DisableExplicitGC was specified on the command line, this function respects that, and silently returns
* (otherwise we would block for quite some time waiting for a natural garbage collection to happen).
* <p>
* This overload uses a default timeout of 100ms.
*/
public static void BlockUntilGC() {
debug(() -> "Starting");
BlockUntilGC(100);
}

/**
* This method calls System.gc, but it blocks until it detects that a garbage collection has run. This
* should only be used when absolutely necessary, for instance, with file based operations.
* There is one caveat, the Epsilon GC does
* nothing, ever. So if we run this method with that GC, we get stuck in an infinite loop until we die due
* to out of memory error. So if that garbage collector is the only one, then we just throw without blocking.
* In such a situation, since no garbage collection will ever occur anyways, whatever was trying to be
* accomplished by calling this method will never happen anyways.
* <p>
* If -XX:+DisableExplicitGC was specified on the command line, this function respects that, and silently returns
* (otherwise we would block for quite some time waiting for a natural garbage collection to happen).
* @param timeout The amount of time in ms to wait before giving up. If the value is 0 or less, the system will only
* try once.
*/
public static void BlockUntilGC(int timeout) {
final long start = System.currentTimeMillis();
final long finish = start + timeout;
debug(() -> "Starting (now: " + start + "; stopping at: " + finish + ")");

RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
@@ -81,6 +103,10 @@ public static void BlockUntilGC() {
break outer;
}
}
if(System.currentTimeMillis() > finish) {
debug(() -> "I've waited too long, so I'm giving up now.");
return;
}
}
}

0 comments on commit 01e37d4

Please sign in to comment.
You can’t perform that action at this time.