Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why have one JVM per application?

Tags:

java

jvm

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 there. Is there one JVM per Java application?

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

like image 212
Apple Grinder Avatar asked Nov 24 '12 07:11

Apple Grinder


People also ask

Can we have 2 JVM in single machine?

Yes,you can install more than one jvm in your PC, because OS loads an instance of jvm (not whole jvm) in RAM. We can call different jvm like JDK 1.4 or JDK 1.6 by setting its path. Multiple JRE (Java Runtime Enviroment) is very possible. Multiple number of JVMs can run on a single machine.

Is there one JVM per Java application?

There's one JVM per Java application. There shouldn't be any connection between them unless you establish one, e.g. with networking. If you're working inside of an IDE, the code you write generally runs in a separate JVM. The IDE will typically connect the separate JVM for debugging.

How many JVM will come if I run multiple Java programs?

Each Java application uses an independent JVM. Each JVM is a separate process, and that means there is no sharing of stacks, heaps, etcetera.

What is multi JVM?

Supports running applications (objects with main methods) and ScalaTest tests in multiple JVMs at the same time. Useful for integration testing where multiple systems communicate with each other.


2 Answers

(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() will effectively kill all applications.
  • If one application 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.

like image 167
Stephen C Avatar answered Nov 15 '22 22:11

Stephen C


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?

like image 26
mgarciaisaia Avatar answered Nov 15 '22 22:11

mgarciaisaia