JVM Internals Series -Part 1
A lot of Java developers tend to be unaware about the basics of the internals of the JVM. This series aims to look at the internals of the JVM and explain in a simple way, what the Java Virtual Machine is and how the JVM functions
What is the Java Virtual Machine ?
When you talk of the JVM there are three aspects which we speak of
- Concrete Implementation
The specification is a concept, Concrete implementations which exist on different platforms and are available from different vendors like IBM, Sun etc are either all software or a mix of hardware and software. A Runtime instance hosts a single running java application.
A runtime instance has one function and that is to run one java application. Whenever a java application runs, a runtime instance is born. Thus the number of runtime instances on a machine are equal to the number of applications which are running. The JVM starts with the invocation of a main() method which needs to be public, static, void and takes a String array as an argument. The main method thus serves as the initial thread for the running application. This can in turn spawn other threads. Inside the VM, threads come in two flavors, daemon and non-daemon. A daemon thread is a thread which is normally used by the VM itself, like the thread which runs the garbage collection. However an application can mark any thread it creates as a daemon. A java application continues to execute until there are any non-daemon threads alive(parallely the VM runtime instance continues to exist). When all non-daemon threads of an application exit, the virtual machine instance exits.
The VM needs memory to store a lot of information, like the bytecode, program arguments, objects which have been instantiated etc. Some of the information stored in the memory is shared across all application threads, while other information may be unique to individual threads.
Each instance of a JVM has one method area and a heap. These areas are shared by all threads running within the instance.
The information regarding the type is loaded from the class files when the JVM loads a class file. This information is stored in the method area. All the objects which are instantiated placed on the heap.
As each new thread is created, it is assigned its own pc register(program counter) and java stack. If the thread is executing a java method (not a native method), the counter in the pc register points to the next instruction to execute. The java stack comprises of frames. A frame consists of the state of one method invocation. When a thread invokes a method, a new frame is pushed on the thread’s stack. On completion of the method execution, the VM pops and discards the frame. No other thread can access another threads pc register or java stack.
This gives a brief overview of the java virtual machine’s architecture. In the next post,I will cover data types and the class loading subsystem.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)