Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gc() method can generate endless loop #21

kmoens opened this issue Sep 8, 2014 · 0 comments

gc() method can generate endless loop #21

kmoens opened this issue Sep 8, 2014 · 0 comments


Copy link

@kmoens kmoens commented Sep 8, 2014


The leak preventor contains a method gc()which ensures that System.gc() effectively performs a garbage collection.

This code can however potentionally result in an endless loop. In the field we have multiple installations where we disable the explict garbage collection with the -XX:+DisableExplicitGC JVM option, this causes that a System.gc() call will never do anything, resulting in an endless loop upon reload of a web application (except if by accident a full GC occurs).

Workaround code which we applied:


     * Unlike <code>{@link System#gc()}</code> this method guarantees that garbage collection has been performed before returning.
    protected static void gc() {
        if (isDisableExplicitGCEnabled()) {
            System.err.println(ClassLoaderLeakPreventor.class.getSimpleName() + ": "
                    + "Skipping GC call since -XX:+DisableExplicitGC is supplied as VM option.");

        Object obj = new Object();
        WeakReference ref = new WeakReference<Object>(obj);
        // noinspection UnusedAssignment
        obj = null;
        while (ref.get() != null) {

     * Check is "-XX:+DisableExplicitGC" enabled.
     * @return true is "-XX:+DisableExplicitGC" is set als vm argument, false otherwise.
    private static boolean isDisableExplicitGCEnabled() {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        List<String> aList = bean.getInputArguments();

        return aList.contains("-XX:+DisableExplicitGC");

Kind regards,
Kenny Moens
CIPAL IT Solutions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.