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

Closed
kmoens opened this Issue Sep 8, 2014 · 0 comments

Comments

Projects
None yet
1 participant
@kmoens
Contributor

kmoens commented Sep 8, 2014

Hello,

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:

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

    /**
     * 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.");
            return;
        }

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

    /**
     * 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