Why have one JVM per application?

前端 未结 5 1034
长情又很酷
长情又很酷 2020-11-30 02:57

I read that each application runs in its own JVM. Why is it so ? Why don\'t they make one JVM run 2 or more apps ?

I read a SO post, but could not get the answers th

5条回答
  •  情书的邮戳
    2020-11-30 03:17

    (I assume you are talking about applications launched via a public static void main(String[]) method ...)

    In theory you can run multiple applications in a JVM. In practice, they can interfere with each other in various ways. For example:

    • The JVM has one set of System.in/out/err, one default encoding, one default locale, one set of system properties, and so on. If one application changes these, it affects all applications.
    • Any application that calls System.exit() kills all applications.
    • If one application thread goes wild, and consumes too much CPU or memory it will affect the other applications too.

    In short, there are lots of problems. People have tried hard to make this work, but they have never really succeeded. One example is the Echidna library, though that project has been quiet for ~10 years. JNode is another example, though they (actually we) "cheated" by hacking core Java classes (like java.lang.System) so that each application got what appeared to be independent versions of System.in/out/err, the System properties and so on1.

    1 - This ("proclets") was supposed to be an interim hack, pending a proper solution using true "isolates". But isolates support stalled, primarily because the JNode architecture used a single address space with no obvious way to separate "system" and "user" stuff. So while we could create APIs that matched the isolate APIs, key isolate functionality (like cleanly killing an isolate) was virtually impossible to implement. Or at least, that was/is my view.

提交回复
热议问题