print
Advertisment
Advertisment

Memory areas in JVM

JVM stands for "Java Virtual Machine." It is a machine that processes instructions just like a physical processor. But in this Java code has to be converted from a . JAVA file first. which the JVM can understand JVMs are updated from time to time with new features, and Java needs a minimum version so that it can run programs. JVM is a software-based machine that is used to run Java programs. It can be installed in any operating system such as Windows, OS X, Linux & Unix. JVM is a part of JRE, which contains many library functions and other files which can be used as a reference for Java programs and we run the programs at JVM, JVM converts byte code into machine code.

java memory management

JVM memory has five parts

  1. Method area in java
  2. Heap memory in java
  3. Stack Memory in java
  4. PC Register in java
  5. Native Method Stack
Heap memory in java | Method area in java | Stack Memory in java | stack and heap memory in java

method area in java

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is similar to the storage area for compiling code of a traditional language or "text" section in an operating system process. It stores per-class structures such as run-time constant pools, field and method data, and code for methods and constructors, including special methods used in class and instance initialization and interfaces initialization.

The method area is created at the virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it. This specification does not mandate the location of the method area or the policies used to manage the compiled code. The method area can be of a fixed size or can be expanded as needed by computation and can be contracted if a large method area becomes unnecessary. The memory for the method area does not need to be contiguous.

A Java Virtual Machine implementation may provide programmer or user control over the initial size of the method area, as well as, in the case of a differentially sized method area, control over the maximum and minimum method area size.

The following exceptional situation is associated with the method field:

If memory cannot be made available in the method area to satisfy the allocation request, the Java Virtual Machine throws an OutOfMemoryError.


Java Heap Memory

Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

Note that the JVM uses more memory than just the heap. For example, Java methods, thread stacks, and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for the allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.

Advertisment

java Stacks Memory

Java Stack memory is used for the execution of a thread. They contain method-specific values that are short-lived and references to other objects in the heap that is getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order.

Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as the method ends, the block becomes unused and becomes available for the next method. Stack memory size is very less compared to Heap memory.

Advertisment

PC Registers

The PC register stores the address of the instruction currently taking place in the Java Virtual Machine. In this, each thread has its own separate PC register.

Native Method Stacks

Advertisment
Advertisment
arrow_upward