Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When multiple java programs run on the same machine

Tags:

java

jvm

Each java application will run in a specific Java Virtual Machine Instance. I am really getting confused on below aspects and Googling has confused me even more. Different articles on different sites.

  1. If I have a web service written in java it will need a JVM instance to run.So can JVM be made a daemon process?

  2. If yes when we run any other java application it will use this instance of JVM or create a new one?

  3. Main memory available in any machine is constant. When we start n java processes simultaneously without providing any initial heap size how is the heap size distributed among processes?

  4. Is there any process that manages n number of JVM instances or is it managed by OS itself?

  5. When stop-the-world happens during an GC are other JVM instances(different threads I assume) affected?

like image 287
Aniket Thakur Avatar asked Aug 23 '13 04:08

Aniket Thakur


People also ask

Can two JVM run on same 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 it possible to run multiple JVM instances concurrently?

Yes you can run multiple VMs on the same machine.

Does each Java program have its own JVM?

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.


2 Answers

1) If I have a web service written in java it will need a JVM instance to run. So can JVM be made a daemon process?

Yes it can. How it is done depends on the O/S and on the web server container itself.

2) If yes when we run any other java application it will use this instance of JVM or create a new one?

No. Each Java application uses an independent JVM.

Each JVM is a separate process, and that means there is no sharing of stacks, heaps, etcetera. (Generally, the only things that might be shared are the read only segments that hold the code of the core JVM and native libraries ... in the same way that normal processes might share code segments.)

3) Main memory available in any machine is constant. When we start n java processes simultaneously without providing any initial heap size how is the heap size distributed among processes?

The mechanism for deciding how big to make the heap if you don't specify a size depends on the JVM / platform / version you are using, and whether you using the "client" or "server" model (for Hotspot JVMs). The heuristic doesn't take account of the number or size of other JVMs.

Reference: https://stackoverflow.com/a/4667635/139985

In practice, you would probably be better off specifying the heap size directly.

4) Is there any process that manages n number of JVM instances or is it managed by OS itself?

Neither. The number of JVM instances is determined by the actions of various things that can start processes; e.g. daemons scripts, command scripts, users typing commands at the command line, etcetera. Ultimately, the OS may refuse to start any more processes if it runs out of resources, but JVMs are not treated any different to other processes.

5) When stop-the-world happens during an GC are other JVM instances(different threads I assume) affected?

No. The JVMs are independent processes. They don't share any mutable state. Garbage collection operates on each JVM independently.

like image 150
Stephen C Avatar answered Oct 14 '22 08:10

Stephen C


  1. see How to Daemonize a Java Program?
  2. new instance of JVM will be created
  3. the same way as memory is shared between all other processes
  4. it is managed by O/S
  5. other instances are not affected

If your instances have to coordinate their work, you can create single main instance which would run/stop other instances.

You did not explain why you need multiple JVM instances. Probably, single instance would work better.

like image 30
Alexei Kaigorodov Avatar answered Oct 14 '22 08:10

Alexei Kaigorodov