Take the 2-minute tour ×

I want to be able to execute the .Jar file, and if the heap space isn't set big enough, it should launch a new JVM with the same .Jar file, but set with a bigger heap space, and then close the first JVM and .Jar.

I've tried using the ProcessBuilder, but I can't get it to work.

It has to work cross platform.


share|improve this question
Why would you start a new JVM instead of simply using a high enough maximum heap size and permgen size? –  Ruben May 20 '11 at 9:32
Presumeably you'll only need to use a bigger heap size once you've run out of memory. Once you've run out of memory, you won't be able to start another process. –  DaveHowes May 20 '11 at 9:35
I have found that the default allocated memory for different JVM on different platforms is different. Therefore, I need to be able to set it on the fly. Furthermore, I want a simple experience, where you just double click the Jar, and it handles the rest. And if no terminal is involved, no flags can be set by the user. –  Mathias Vielwerth May 20 '11 at 23:23

4 Answers 4

I'd do this kind of work in an outer script file - in pseudo code:

$heap := 128
$ok := true
do {
  exitCode = java -Xmx$heapM -jar myApp.jar
  if (exitCode = OOME) {
    heap += 128
    $ok := false

Catch OOME and exiting with a custom code should always be possible. There's one problem with this approach - if the $heap value exceeds the maximum heap space that is possible for the target system (example: ~1.4GByte on Win32 systems) then it will not terminate.

Note: this is just an answer to the question - usually one would assign a high amount of memory and/or fight the memory leaks - but I don't know the actual requirments/restrictions

share|improve this answer

You can launch java with an initial heap size, and also specify a maximum heap size which will be only be used as required. I'm not sure what you're trying to do but it might emulate the behaviour you want?

java -Xms256m -Xmx1g -jar myapp.jar

In this example you start with 256M, if the app needs more memory it will take it, incrementally, up until 1G.

share|improve this answer
Do you mean,-Xms=256m -Xmx=1g or -ms=256m -mx=1g –  Peter Lawrey May 20 '11 at 10:41
Ah - spaces typo - no space required, thanks for spotting. –  Joel May 20 '11 at 14:39
no = required either. ;) –  Peter Lawrey May 20 '11 at 14:42
I do know how to set the heap space. Else I wouldn't be able to run the code myself. The question is, how do you relaunch an already running JVM with more heap space. –  Mathias Vielwerth May 20 '11 at 23:24
I still don't understand why you don't just set the maximum available heap space for each process, with a smaller initial size. –  Joel May 27 '11 at 7:16
up vote 3 down vote accepted

I have found the solution, and it works cross platform. To restart the JVM from code, use the following. This answer is taken from another question I found after hours of search in here. If you want, you can follow it with an System.exit(0), to terminate the JVM that started the new process, after a call to this method.

public static void startSecondJVM() throws Exception {
    String separator = System.getProperty("file.separator");
    String classpath = System.getProperty("java.class.path");
    String path = System.getProperty("java.home")
            + separator + "bin" + separator + "java";
    ProcessBuilder processBuilder = 
            new ProcessBuilder(path, "-Xmx1024m", "-cp",
    Process process = processBuilder.start();
share|improve this answer

You might try combining these two sources.


Makes attempts to recover from an OutOfMemoryError.

/*License - LGPL
<h3>Recovery from an OutOfMemory Error</h3>
<p>The JavaDocs for Error state, in the first sentence..

<blockquote>"An Error is a subclass of Throwable that indicates
serious problems that a reasonable application should
not try to catch."</blockquote>

<p>This advice has led to the fallacy that an OutOfMemoryError
should not be caught and dealt with.But this demo. shows
that it is quite easy to recover to the point of providing
the user with meaningful information, and advice on how to

<p>I aim to make my applications 'unreasonable'.;-)

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.Timer;

import javax.swing.border.EmptyBorder;

import java.util.ArrayList;

/** A demo. showing recovery from an OutOfMemoryError.
Our options once an OOME is encountered are relatively
few, but we can still warn the end user and provide
advice on how to correct the problem.
@author Andrew Thompson */
public class MemoryRecoveryTest {

    public static void main(String[] args) {
        // reserve a buffer of memory
        byte[] buffer = new byte[2^10];
        ArrayList<Object> list = new ArrayList<Object>();
        final JProgressBar memory = new JProgressBar(
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                    (int)Runtime.getRuntime().freeMemory() );
        Timer timer = new Timer(500, listener);

        JDialog dialog = new JDialog();
        dialog.setTitle("Available Memory");
        JPanel memoryPanel = new JPanel();
        memoryPanel.setBorder(new EmptyBorder(25,25,25,25));
        dialog.add( memoryPanel );
        dialog.addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent we) {
        } );

        // prepare a memory warning panel in advance
        JPanel memoryWarning = new JPanel();
        memoryWarning.add( new JLabel(
            "<HTML><BODY>There is not enough memory to" +
            " complete the task!<BR> Use a variant " +
            " of the application that assigns more memory.") );

        try {
            // do our 'memory intensive' task
            while(true) {
                list.add( new Object() );
        } catch(OutOfMemoryError oome) {
            // provide the VM with some memory 'breathing space'
            // by clearing the buffer
            buffer = null;
            // tell the user what went wrong, and how to fix it
                "Out of Memory!",


Ensures a Process is started with a memory size specified.

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import java.io.File;

class IWantToBeBig {

        if (args.length==0) {
            ProcessBuilder pb = new ProcessBuilder(
            pb.directory(new File("."));
            Process process = pb.start();
            System.out.println("Exit value: " + process.exitValue());
        } else {
            Runnable r = new Runnable() {
                public void run() {
                        "Max Memory: " +
                        Runtime.getRuntime().maxMemory() +
                        " bytes.");
share|improve this answer

Your Answer


