JVM or Java Virtual Machine
Java Virtual Machine is a specification to provide the runtime environment on which a bytecode can be executed. It provide class loader to load a class, runtime and executed the bytecode. The following is the architecture diagram of JVM.
Image is from : javatpoint.com
Classloader − Loads the class file into the JVM.
Class Area − Storage areas for a class elements structure like fields, method data, code of method etc.
Heap − Runtime storage allocation for objects.
Stack − Storage for local variables and partial results. A stack contains frames and allocates one for each thread. Once a thread gets completed, this frame also gets destroyed. It also plays roles in method invocation and returns.
PC Register − Program Counter Register contains the address of an instruction that JVM is currently executing.
Native method stack − It contains all the native methods used by the application.
Execution Engine − It has a virtual processor, interpreter to interpret bytecode instructions one by one and a JIT, just in time compiler.
Parameter | Stack Memory | Heap Space |
Application | Stack is used in parts, one at a time during execution of a thread | The entire application uses Heap space during runtime |
Size | Stack has size limits depending upon OS, and is usually smaller than Heap | There is no size limit on Heap |
Storage | Stores only primitive variables and references to objects that are created in Heap Space | All the newly created objects are stored here |
Order | It's accessed using Last-in First-out (LIFO) memory allocation system | This memory is accessed via complex memory management techniques that include Young Generation, Old or Tenured Generation, and Permanent Generation. |
Life | Stack memory only exists as long as the current method is running | Heap space exists as long as the application runs |
Efficiency | Much faster to allocate when compared to heap | Slower to allocate when compared to stack |
Allocation | This Memory is automatically allocated and deallocated when a method is called and returned, respectively | Heap space is allocated when new objects are created and deallocated by Garbage Collector when they're no longer referenced |
Comments