3

I understand that each java process runs in its own JVM. For example when I run jcmd in my machine, I see

21730 sun.tools.jcmd.JCmd
77558 /usr/local/opt/jenkins-lts/libexec/jenkins.war --httpListenAddress=127.0.0.1 --httpPort=8080
99974
99983 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA.app/Contents/lib/asm-all-7.0.1.jar:/Applications/IntelliJ IDEA.app/Contents/lib/lz4-java-1.6.0.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/java/lib/aether-connector-basic-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/java/lib/plexus-utils-3.0.22.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/java/lib/aether-api-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/java/lib/javac2.jar:/Applications/IntelliJ IDEA.app/Contents/lib/util.jar:/Applications/IntelliJ IDEA.app/Contents/lib/platform-api.jar:/Applications/IntelliJ IDEA.app/Contents/lib/qdox-2.0-M10.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jna.jar:/Applications/IntelliJ IDEA.app/Contents/lib/trove4j.jar:/Applications/IntelliJ IDEA.app/Contents/lib/nanoxml-2.2.3.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jdom.jar:/Applications/IntelliJ IDEA.app/Contents/lib/netty-common-4.1.41.Final.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/java/lib/aet

How is the JVM created per app ? Like what happens when I start jenkins with java -jar jenkins.war. Does some process copy over JVM stuff from JRE folder and initialize an instance of JVM ?

Ravi
  • 133
  • 10
  • 2
    The jvm process is created the same way all processes are created on the operating system. Are you asking how processes are created in general or is there some Java specific aspect to the question? – Joni Jul 06 '20 at 00:14
  • ok..so I guess the JVM is the program and each java app creates a JVM process and runs on top of it – Ravi Jul 06 '20 at 00:19
  • 1
    The JVM is actually the app, from the perspective of the host OS. The JVM then uses its class loader to load the classes of what you think of as your app. But your Question is intriguing from the standpoint of how the JDK/JRE is used simultaneously by multiple apps running. That question is somewhat moot, as Oracle now expects us to wrap a JVM runtime within our app bundle, ideally a slimmed-down JVM runtime via the Java Platform Module System. But the question is nevertheless intriguing. Gets my upvote. – Basil Bourque Jul 06 '20 at 00:47
  • 1
    See [1](https://stackoverflow.com/a/26025656/3448419), [2](https://stackoverflow.com/a/30094517/3448419), [3](https://stackoverflow.com/a/24558676/3448419) – apangin Jul 06 '20 at 07:39

1 Answers1

2

When you start a program like java, the operating system creates a "process". A process is the representation of a live, running program. The process concept is what allows you to run several copies of a program at the same time. Each process has its own private memory space and system resources like open files or network connections. Each process can load a different set of dynamically linked libraries. With Java, much of the jvm is implemented in shared libraries, which the launcher program "java" loads in at run time.

The details are OS dependent and become complicated fast.

One of the things that happen when the process is started is that the executable file is mapped into memory. The CPU cannot execute instructions that are on disk or other external storage, so the program "text" has to be copied from disk into main memory first. Mapping the file into memory simplifies this and makes it more efficient: If the CPU needs to access a memory location that's not actually in RAM, the memory manager unit (MMU) issues a "page fault". The page fault causes data to be loaded into RAM. This is more efficient than simply copying the program text into RAM (what if not all text is needed all the time) and also simplifies the overall system (the virtual memory system is already needed for other OS features)

Joni
  • 108,737
  • 14
  • 143
  • 193