Example usage for java.lang Runtime runFinalization

List of usage examples for java.lang Runtime runFinalization

Introduction

In this page you can find the example usage for java.lang Runtime runFinalization.

Prototype

public void runFinalization() 

Source Link

Document

Runs the finalization methods of any objects pending finalization.

Usage

From source file:Main.java

public static void main(String[] args) {

    Runtime runTime = Runtime.getRuntime();
    runTime.runFinalization();
    System.out.println("Done.");

}

From source file:Mem.java

public static void gc() {
    Runtime r = Runtime.getRuntime();
    r.runFinalization();
    r.gc();
}

From source file:ffx.HeadlessMain.java

/**
 * Complete initializations.//from   w w  w .ja v  a2s  . co m
 *
 * @param commandLineFile a {@link java.io.File} object.
 * @param argList a {@link java.util.List} object.
 * @param logHandler a {@link ffx.ui.LogHandler} object.
 */
public HeadlessMain(File commandLineFile, List<String> argList, LogHandler logHandler) {
    // Start a timer.
    stopWatch.start();

    // Construct the MainPanel, set it's LogHandler, and initialize then it.
    mainPanel = new MainPanel();
    logHandler.setMainPanel(mainPanel);
    mainPanel.initialize();

    // Open the supplied script file.
    if (commandLineFile != null) {
        if (!commandLineFile.exists()) {
            /**
             * See if the commandLineFile is an embedded script.
             */
            String name = commandLineFile.getName();
            name = name.replace('.', '/');
            String pathName = "ffx/scripts/" + name;
            ClassLoader loader = getClass().getClassLoader();
            URL embeddedScript = loader.getResource(pathName + ".ffx");
            if (embeddedScript == null) {
                embeddedScript = loader.getResource(pathName + ".groovy");
            }
            if (embeddedScript != null) {
                try {
                    commandLineFile = new File(FFXClassLoader.copyInputStreamToTmpFile(
                            embeddedScript.openStream(), commandLineFile.getName(), ".ffx"));
                } catch (Exception e) {
                    logger.warning("Exception extracting embedded script " + embeddedScript.toString() + "\n"
                            + e.toString());
                }
            }
        }
        if (commandLineFile.exists()) {
            mainPanel.getModelingShell().setArgList(argList);
            mainPanel.open(commandLineFile, null);
        } else {
            logger.warning(format("%s was not found.", commandLineFile.toString()));
        }
    }

    /**
     * Print start-up information.
     */
    if (logger.isLoggable(Level.FINE)) {
        StringBuilder sb = new StringBuilder();
        sb.append(format("\n Start-up Time (msec): %s.", stopWatch.getTime()));
        Runtime runtime = Runtime.getRuntime();
        runtime.runFinalization();
        runtime.gc();
        long occupiedMemory = runtime.totalMemory() - runtime.freeMemory();
        long KB = 1024;
        sb.append(format("\n In-Use Memory   (Kb): %d", occupiedMemory / KB));
        sb.append(format("\n Free Memory     (Kb): %d", runtime.freeMemory() / KB));
        sb.append(format("\n Total Memory    (Kb): %d", runtime.totalMemory() / KB));
        logger.fine(sb.toString());
    }
}

From source file:ffx.ui.UIDataConverter.java

/**
 * Rather verbose output for timed File Operations makes it easy to grep log
 * files for specific information.// w  w w.  j  a v a  2  s  .c  om
 */
private void startTimer() {
    Runtime runtime = Runtime.getRuntime();
    if (gc) {
        runtime.runFinalization();
        runtime.gc();
    }
    occupiedMemory = runtime.totalMemory() - runtime.freeMemory();
    time -= System.nanoTime();
}

From source file:ffx.ui.UIDataConverter.java

private void stopTimer(FFXSystem ffxSystem) {
    time += System.nanoTime();/*from  w ww . ja  va  2  s.  c  om*/
    logger.log(Level.INFO, " Opened {0} with {1} atoms.\n File Op Time  (msec): {2}",
            new Object[] { ffxSystem.toString(), ffxSystem.getAtomList().size(), time * 1.0e-9 });
    Runtime runtime = Runtime.getRuntime();
    if (gc) {
        runtime.runFinalization();
        runtime.gc();
        long moleculeMemory = (runtime.totalMemory() - runtime.freeMemory()) - occupiedMemory;
        logger.log(Level.INFO, " System Memory  (Kb): {0}", moleculeMemory / KB);
    }
    occupiedMemory = runtime.totalMemory() - runtime.freeMemory();
    if (gc) {
        logger.log(Level.INFO, " Memory In Use  (Kb): {0}", occupiedMemory / KB);
    }
}

From source file:ffx.Main.java

/**
 * Main does some window initializations.
 *
 * @param commandLineFile a {@link java.io.File} object.
 * @param argList a {@link java.util.List} object.
 */// ww w  .j  a v a  2  s.  c  o m
public Main(File commandLineFile, List<String> argList) {
    super("Force Field X");
    // Start the clock.
    stopWatch.start();
    setVisible(false);

    // Create the MainPanel and MainMenu, then add them to the JFrame
    java.awt.Toolkit.getDefaultToolkit().setDynamicLayout(true);
    mainPanel = new MainPanel(this);
    logHandler.setMainPanel(mainPanel);
    add(mainPanel);
    mainPanel.initialize();
    setJMenuBar(mainPanel.getMainMenu());
    // Set the Title and Icon
    setTitle("Force Field X");
    URL iconURL = getClass().getClassLoader().getResource("ffx/ui/icons/icon64.png");
    ImageIcon icon = new ImageIcon(iconURL);
    setIconImage(icon.getImage());
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (mainPanel != null) {
                mainPanel.exit();
            }
            System.exit(0);
        }
    });
    // This is a hack to get GraphicsCanvis to initialize on some
    // platform/Java3D combinations.
    mainPanel.setPanel(MainPanel.KEYWORDS);
    setVisible(true);
    mainPanel.setPanel(MainPanel.GRAPHICS);
    // Mac OS X specific features that help Force Field X look native
    // on Macs. This needs to be done after the MainPanel is created.
    if (SystemUtils.IS_OS_MAC_OSX) {
        osxAdapter = new OSXAdapter(mainPanel);
    }

    // Finally, open the supplied file if necessary.
    if (commandLineFile != null && !commandLineFile.exists()) {
        /**
         * See if the commandLineFile is an embedded script.
         */
        String name = commandLineFile.getName();
        name = name.replace('.', '/');
        String pathName = "ffx/scripts/" + name;
        ClassLoader loader = getClass().getClassLoader();
        URL embeddedScript = loader.getResource(pathName + ".ffx");
        if (embeddedScript == null) {
            embeddedScript = loader.getResource(pathName + ".groovy");
        }
        if (embeddedScript != null) {
            try {
                commandLineFile = new File(FFXClassLoader.copyInputStreamToTmpFile(embeddedScript.openStream(),
                        commandLineFile.getName(), ".ffx"));
            } catch (Exception e) {
                logger.info(String.format(" The embedded script %s could not be extracted.", embeddedScript));
            }
        }
    }

    if (commandLineFile != null) {
        if (commandLineFile.exists()) {
            mainPanel.getModelingShell().setArgList(argList);
            mainPanel.open(commandLineFile, null);
        } else {
            logger.warning(format("%s was not found.", commandLineFile.toString()));
        }
    }

    if (logger.isLoggable(Level.FINE)) {
        StringBuilder sb = new StringBuilder();
        sb.append(format("\n Start-up Time (msec): %s.", stopWatch.getTime()));
        Runtime runtime = Runtime.getRuntime();
        runtime.runFinalization();
        runtime.gc();
        long occupiedMemory = runtime.totalMemory() - runtime.freeMemory();
        long KB = 1024;
        sb.append(format("\n In-Use Memory   (Kb): %d", occupiedMemory / KB));
        sb.append(format("\n Free Memory     (Kb): %d", runtime.freeMemory() / KB));
        sb.append(format("\n Total Memory    (Kb): %d", runtime.totalMemory() / KB));
        logger.fine(sb.toString());
    }
}

From source file:er.extensions.ERXExtensions.java

/**
 * Forces the garbage collector to run. The
 * max loop parameter determines the maximum
 * number of times to run the garbage collector
 * if the memory footprint is still going down.
 * In normal cases you would just need to call
 * this method with the parameter 1. If called
 * with the parameter 0 the garbage collector
 * will continue to run until no more free memory
 * is available to collect. <br/>//from www. j a v  a  2  s .c  o  m
 * <br/>
 * Note: This can be a very costly operation and
 * should only be used in extreme circumstances.
 * @param maxLoop maximum times to run the garbage
 *      collector. Passing in 0 will cause the
 *      collector to run until all free objects
 *      have been collected.
 */
public static void forceGC(int maxLoop) {
    if (_log.isDebugEnabled())
        _log.debug("Forcing full Garbage Collection");
    Runtime runtime = Runtime.getRuntime();
    long isFree = runtime.freeMemory();
    long wasFree;
    int i = 0;
    do {
        wasFree = isFree;
        runtime.gc();
        isFree = runtime.freeMemory();
        i++;
    } while (isFree > wasFree && (maxLoop <= 0 || i < maxLoop));
    runtime.runFinalization(); //TODO: should this be inside the loop?
}

From source file:StreamFlusher.java

private void _runGC(boolean inGUI, PseudoTerminalInternalFrame terminal, Runtime runtime) {
    long memInUseAfter = getMemInUse(runtime), memInUseBefore = Long.MAX_VALUE;
    for (int j = 0; (memInUseAfter < memInUseBefore) && (j < 10); // KRB: magic number
            j++) {/*from   w w w  . j  ava2 s.co m*/
        runtime.runFinalization();
        runtime.gc();
        Thread.currentThread().yield();

        memInUseBefore = memInUseAfter;
        memInUseAfter = getMemInUse(runtime);
        if (inGUI) {
            terminal.appendToHistory(memInUseBefore + "    " + memInUseAfter);
        } else {
            System.out.println(memInUseBefore + "    " + memInUseAfter);
        }
    }
}