内存中发生了什么,一个新进程是否总是在 Java 中打开一个新的 JVM?

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

  1. 当我们使用 ProccessBuilder.start()RunTime.getRunTime.exec() 启动新进程时,Java 会发生什么?

它会用新进程的新堆栈和堆打开一个新的 JVM 吗? 当我们创建和 运行 一个新进程时是否打开了一个新的 JVM?

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

来自 here “启动操作系统进程高度依赖于系统。”

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?

不,只会创建一个新的 system process(类似于使用 shell 系统启动一个新进程,如 $ programC:\> program.exe)。

例如运行:

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

产生一个新的系统进程(独立于 JVM 运行时)

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

即使你不等待,JVM结束它们的执行但进程还在运行

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

有输出

End

但是系统进程

[josejuan@plata ~]$ ps -AF | grep sleep
josejuan   80866       1  0  2135   684   1 08:37 tty1     00:00:00 sleep 10