Java ProcessBuilder memory

北战南征 提交于 2019-12-24 10:36:51

问题


I was wondering if anybody knew with any certainty whether ProcessBuilder/Runtime.exec() executes inside the space of the JVM's memory or whether it uses completely separate system memory and somehow sends the output to Java. I could not find any documentation on the subject.

I assume it is the former due to security issues and being able to read output, but I would like to make absolutely sure.


回答1:


The new process runs outside the Java process that started it. Allocation of memory to the new process is managed by the operating system, as part of process management.

The Java class ProcessBuilder, which provides an interface for starting and communicating with the new process, runs inside the Java process.




回答2:


Seems pretty clear that exec launches a new process, or program for those not versed in operating system terminology. That's why it has input output facilities, ability to set the environment, and ability to wait on the external program returning.

The first line of the javadoc says it all.

Executes the specified string command in a separate process.

The command argument is parsed into tokens and then executed as a command in a
separate process. The token parsing is done by a StringTokenizer created by the 
call:

     new StringTokenizer(command)


with no further modifications of the character categories. This method has exactly
the same effect as exec(command, null).



回答3:


From the concurrency reference of Java SE, it is said that:

A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

If you are interested in the internals, check the UNIXProcess class from the openJDK.



来源:https://stackoverflow.com/questions/10883729/java-processbuilder-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!