2

Can anyone please tell me why multiple instances will be created if we run multiple java programs in a single machine? Why can't all programs share a single jvm and will java libraries load separately to all jvm instances?

eldo
  • 1,327
  • 2
  • 16
  • 27
Rakesh SKadam
  • 378
  • 1
  • 2
  • 18

3 Answers3

4

Can anyone please tell me why multiple instances will be created if we run multiple java programs in a single machine?

When you run any program more than once it creates multiple instances of that program. This is not unique to Java.

Why can't all programs share a single jvm

Your programs should share a single JVM, however this means you should only start one JVM and tell it which application to run. Whether you want to do this or not is another question. SHaring application add complexity and in fact the trend is to turn single monoliths into multiple microservices

Some reasons to split rather than combine JVMs.

  • the pause time of standard garbage collectors increases with the size of the heap used. Splitting the applications will mean more, smaller pauses.
  • tuning the garbage collection for a simple work load is easier. If you have mixed work loads one might want a large young space and the other a small young space. The more mixed the work load, the harder the JVM is to tune.
  • if you have a resource leak in your application, the only way to fix this might be to restart the JVM. If each application is in it's own JVM, you need not stop other applications.
  • you might have code you can bring down the JVM (fill up the memory, crash the JVM, or leave it in unusable state) Multiple JVMs limit the impact of such a failure.
  • forces you to a clear separation between key components which improves maintainability. This can be done in a monolith but needs disciple.

and will java libraries load separately to all jvm instances?

The JVM memory maps the JARs into memory and it is the OS, not the JVM, which then decides whether or how JARs are shared between processes.

However, each JVM which needs a class loads it and sets it's copy of the static fields for example.

i mean to say how does jvm loads common libraries in memory because if two jvm loads same classes in memory then there might be conflict while accessing that class object

Each process has it's own memory space. If two JVM load the same class, these two copies have no interaction with one another and there is no opportunity for conflict.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

In theory one jvm can be host of multiple java applications but in action there can be a lot of interferes.

Based on documentation:

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.

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
1

Because you don't want all Java programs to crash if one kills the JVM. And yes the Java Libraries will load seperately. But the Java program tells the JVM which libraries to load with the "import" keyword.

RalleYTN
  • 138
  • 10
  • i mean to say how does jvm loads common libraries in memory because if two jvm loads same classes in memory then there might be conflict while accessing that class object – Rakesh SKadam Oct 11 '16 at 12:55