io.dropwizard.servlets.tasks.GarbageCollectionTask Maven / Gradle / Ivy
package io.dropwizard.servlets.tasks;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
/**
* Performs a full JVM garbage collection (probably).
*/
public class GarbageCollectionTask extends Task {
private final Runtime runtime;
/**
* Creates a new GarbageCollectionTask.
*/
public GarbageCollectionTask() {
this(Runtime.getRuntime());
}
/**
* Creates a new GarbageCollectionTask with the given {@link Runtime} instance.
*
* Use {@link GarbageCollectionTask#GarbageCollectionTask()} instead.
*
* @param runtime a {@link Runtime} instance
*/
public GarbageCollectionTask(Runtime runtime) {
super("gc");
this.runtime = runtime;
}
@Override
@SuppressWarnings("CallToSystemGC")
public void execute(Map> 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 int parseRuns(Map> parameters) {
final List runs = parameters.get("runs");
if (runs == null || runs.isEmpty()) {
return 1;
} else {
try {
return Integer.parseInt(runs.get(0));
} catch (NumberFormatException ignored) {
return 1;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy