Java Virtual Mechine. How it works?
- First jvm looks for main method in class.if exists then it will start executing from that
- Inside the Java virtual machine, threads come in two flavors: daemon and non- daemon. A daemon thread is ordinarily a thread used by the virtual machine itself, such as a thread that performs garbage collection. The application, however, can mark any threads it creates as daemon threads. The initial thread of an application–the one that begins at main()–is a non- daemon thread.
- A Java application continues to execute (the virtual machine instance continues to live) as long as any non-daemon threads are still running. When all non-daemon threads of a Java application terminate, the virtual machine instance will exit. If permitted by the security manager, the application can also cause its own demise by invoking the exit() method of class Runtime or System.
The Architecture of the Java Virtual Machine
- All jvm’s contains two components
class loader subsystem: a mechanism for loading types (classes and interfaces) given fully qualified names.
execution engine: a mechanism responsible for executing the instructions contained in the methods of loaded classes.
- When a Java virtual machine runs a program, it needs memory to store many things, including Bytecodes, info from loaded class files ,objects the program instantiates,parameters to methods,return values, local variables, and intermediate results of computations.
- Different implementations of the virtual machine can have very different memory constraints. Some implementations may have a lot of memory in which to work, others may have very little. Some implementations may be able to take advantage of virtual memory, others may not. The abstract nature of the specification of the runtime data areas helps make it easier to implement the Java virtual machine on a wide variety of computers and devices.
- Different java programs has different jvm instances. If I execute one class then one separate jvm instance will handle that
- When the virtual machine loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the method area places all objects the program instantiates onto the heap.
- As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. A thread’s Java stack stores the state of Java (not native) method invocations for the thread.
- The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread’s Java stack. When the method completes, the virtual machine pops and discards the frame for that metho
- Java virtual machine contains two kinds of class loaders: java.lang.ClassLoader
bootstrap class loader, user-defined class loaders.
- Each thread of a running program has its own program counter register, or program counter, which is created when the thread is started. The pc register is one word in size, so it can hold both a native pointer and a returnAddress. As a thread executes a Java method, the pc register contains the address of the current instruction being executed by the thread. An “address” can be a native pointer or an offset from the beginning of a method’s bytecodes. If a thread is executing a native method, the value of the pc register is undefined.
- Two stacks here
Java Stack: maintained by JVM,
Native stack: depends.
hi everyone please comment on my post. if any thing wrong in my post please feel free to correct it, Thank you.