Skip to content
Advertisement

What happens in the memory and does a new process always open a new JVM in Java?

  1. What happens in Java when we start a new process using ProccessBuilder.start() or RunTime.getRunTime.exec()?

Will it open a new JVM with a new stack & heap of the new process? Does a new JVM is opened when we create and run a new process?

Advertisement

Answer

What happens in Java when we start a new process using ProccessBuilder.start() or RunTime.getRunTime.exec()?

From here “Starting an operating system process is highly system-dependent.”

Will it open a new JVM with a new stack & heap of the new process? Does a new JVM is opened when we create and run a new process?

No, simply a new system process will be created (similar to start a new process using the system shell like $ program or C:> program.exe).

E.g. running:

Process p = new ProcessBuilder().command("sleep", "10").start();
p.waitFor();
System.out.println("End");

produce a new system process (independent of the JVM runtime)

$ ps -AF | grep sleep
josejuan   80777   80751  0  2135   732   3 08:36 tty1     00:00:00 sleep 10

even if you do not wait, the JVM ends their execution but the process is still running

Process p = new ProcessBuilder().command("sleep", "10").start();
System.out.println("End");

with output

End

but system process

[josejuan@plata ~]$ ps -AF | grep sleep
josejuan   80866       1  0  2135   684   1 08:37 tty1     00:00:00 sleep 10
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement