Example usage for java.lang Runtime gc

List of usage examples for java.lang Runtime gc

Introduction

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

Prototype

public native void gc();

Source Link

Document

Runs the garbage collector in the Java Virtual Machine.

Usage

From source file:pl.edu.icm.visnow.system.main.VisNow.java

public long getMemoryAvailable() {
    Runtime r = Runtime.getRuntime();
    r.gc();
    long total = r.totalMemory();
    long free = r.freeMemory();
    long used = total - free;
    return (memoryMax - used);
}

From source file:org.apache.tez.dag.app.TestMockDAGAppMaster.java

private void checkMemory(String name, MockDAGAppMaster mockApp) {
    long mb = 1024 * 1024;

    //Getting the runtime reference from system                                    
    Runtime runtime = Runtime.getRuntime();

    System.out.println("##### Heap utilization statistics [MB] for " + name);

    runtime.gc();

    //Print used memory                                                            
    System.out.println("##### Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

    //Print free memory                                                            
    System.out.println("##### Free Memory:" + runtime.freeMemory() / mb);

    //Print total available memory                                                 
    System.out.println("##### Total Memory:" + runtime.totalMemory() / mb);

    //Print Maximum available memory                                               
    System.out.println("##### Max Memory:" + runtime.maxMemory() / mb);
}

From source file:org.apache.ojb.performance.PerfMain.java

/** Call this to begin the performance test. */
public void startPerfTest(String[] args) throws Exception {
    ArrayList testList = null;//from   w  ww.  j  av a  2 s.  c  om
    try {
        // comma separated list of the PerfTest implementation classes
        if (args.length > 0) {
            StringTokenizer tok = new StringTokenizer(args[0], ",");
            testList = new ArrayList();
            while (tok.hasMoreTokens()) {
                testList.add(tok.nextToken().trim());
            }
        } else {
            throw new IllegalArgumentException("No test handles found!");
        }
        // number of test loops
        if (args.length > 1) {
            testLoops = args.length > 1 ? Integer.parseInt(args[1]) : 1;
        }
        // number of threads
        if (args.length > 2) {
            concurrentThreads = Integer.parseInt(args[2]);
        }
        // number of insert/fetch/delete loops per thread
        if (args.length > 3) {
            iterationsPerThread = Integer.parseInt(args[3]);
        }
        // run in stress mode
        if (args.length > 4) {
            useStressMode = Boolean.valueOf(args[4]).booleanValue();
        }
        // log mode
        if (args.length > 5) {
            logAll = Boolean.valueOf(args[5]).booleanValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println();
        System.err.println("Usage of PerfMain:"
                + "java -classpath CLASSPATH org.apache.ojb.performance.PerfMain"
                + " [comma separated list of PerfTest implementation classes]" + " [number of test loops]"
                + " [number of threads]" + " [number of insert/fetch/delete loops per thread]"
                + " [boolean - run in stress mode]"
                + " [boolean - if 'true' detailed log messages will be print]");
        System.err.println();
        System.err.println("Example: java -classpath"
                + " CLASSPATH org.apache.ojb.performance.PerfMain org.MyPerfTest 3 10 500 false");
    }

    if (logAll)
        printer().println("                                                    " + EOL
                + "Start OJB performance-test framework - running " + testLoops + " loops" + EOL
                + "-------------------------------------------------------");

    PerfRunner test;
    for (int i = 0; i < testLoops; i++) {
        Runtime rt = Runtime.getRuntime();
        long freeMem;
        if (logAll)
            printer().println(" Loop " + (i + 1));

        if (i % 2 == 0) {
            for (int j = 0; j < testList.size(); j++) {
                String perfTest = (String) testList.get(j);
                Class testHandle = Class.forName(perfTest);
                test = new PerfRunner(testHandle);
                test.registerPerfMain(this);

                rt.gc();
                Thread.sleep(300);
                rt.freeMemory();
                rt.gc();
                Thread.sleep(100);
                freeMem = rt.freeMemory();
                test.performTest();
                freeMem = (freeMem - rt.freeMemory()) / 1024;
                if (logAll)
                    printer().println(" allocated memory=" + freeMem + "kb");
                // rt.gc();
            }
        } else {
            for (int j = (testList.size() - 1); j >= 0; j--) {
                String perfTest = (String) testList.get(j);
                Class testHandle = Class.forName(perfTest);
                test = new PerfRunner(testHandle);
                test.registerPerfMain(this);

                rt.gc();
                Thread.sleep(300);
                rt.freeMemory();
                rt.gc();
                Thread.sleep(100);
                freeMem = rt.freeMemory();
                test.performTest();
                freeMem = (freeMem - rt.freeMemory()) / 1024;
                if (logAll)
                    printer().println(" allocated memory: " + freeMem + " kb");
                // rt.gc();
            }
        }
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

public void printImage(String filename, float scale, CanvasCommon canvas, Dimension graphsize) {
    try {/*from   w  w w  .jav a  2 s  .  co m*/
        int h_margin = 72, v_margin = 72;
        BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        if (scale > 0)
            g2.scale(scale, scale);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, image.getWidth(), image.getHeight());
        // canvas.paint(g2);
        Color bgsave = canvas.getBackground();
        canvas.setBackground(Color.white);
        canvas.paintComponent(g2);
        canvas.setBackground(bgsave);
        g2.dispose();
        ImageIO.write(image, "jpeg", new File(filename));
        image = null;
        Runtime r = Runtime.getRuntime();
        r.gc();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

public byte[] printImage(float scale, CanvasCommon canvas, Dimension graphsize, String format)
        throws IOException {
    int h_margin = 72, v_margin = 72;
    BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    if (scale > 0)
        g2.scale(scale, scale);/*w w w  .  j a v  a 2  s .  c  o m*/
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, image.getWidth(), image.getHeight());
    // canvas.paint(g2);
    Color bgsave = canvas.getBackground();
    boolean edsave = canvas.editable;
    canvas.editable = false;
    canvas.setBackground(Color.white);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    canvas.paintComponent(g2);
    canvas.setBackground(bgsave);
    canvas.editable = edsave;
    g2.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, format, baos);
    image = null;
    Runtime r = Runtime.getRuntime();
    r.gc();
    return baos.toByteArray();
}

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++) {//  w  w  w .  j ava  2s.  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);
        }
    }
}