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
(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:
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.
Reason to have one JVM pre application, basically same having OS process per application. Here are few reasons why to have a process per application.
Well some applications such a Chrome go even further creating multiple processes to isolate different tabs and plugins.
Speaking of Java there are few more reasons not to share JVM.
Though there are several cases there JVM is actually shared between application:
But in practice, even in server side java, it usually better to use JVM (or several) per applications, for reasons mentioned above.
It depends with your application.
Waratek provides Hosting multiple Java applications within a single JVM Just check that.
For isolating execution contexts.
If one of the processes hangs, or fails, or it's security is compromised, the others don't get affected.
I think having separate runtimes also helps GC, because it has less references to handle than if it was altogether.
Besides, why would you run them all in one JVM?
Java Application Servers, like JBoss, are design to run many applications in one JVM