Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, working of a garbage collector, things that are managed by JVM (Java Virtual Machine).
It is necessary for a programmer to understand the working of memory management to create a high performance-based program that will not crash or if got crashed, they will aware of how to debug and overcome from the crash.
In every coding language, memory management plays a vital role in the allocation and deallocation of memory areas because it needs critical care. In Java, the JVM and the Garbage Collector manages the memory allocation.
Structure of Java Memory Areas
The JVM defines various runtime data areas that are used when we run our code. Some of the data areas are created when JVM started and destroyed in the presence of JVM, and data areas created during the initialization of the thread get destroyed when the thread exists.
Heap:
- Heap represents the runtime data area that stores the actual object in the memory. It is created during the virtual machine start-up.
- The memory is allocated to every class instance. The size of the heap depends upon the system’s configuration.
- When a new keyword is used, the object is assigned a memory area in a heap, but the reference of the object exists onto the stack.
Method Areas
- Method area store class structures, method data, and constructor field data, including the special methods used in class.
- It is created when the JVM starts.
- Mehtod area is logically a part of the heap area, but it may or may not be garbage collected.
- The size of the method may be fixed or expanded as required by the computation and contracted if the method area is in use unnecessarily.
JVM Stacks
- Each and every thread is having its own stack created at the same time as that of the thread.
- Every stack classified into different parts called frames. These frames are used to store data and partial results and to perform dynamic linking, return value for methods, and dispatch exception.
- Each and every stack’s frame divided into three different parts.
- Local variable storage area
- Operand stack
- Frame data.
- All the local variables going to store in the Local variable storage area, any operations on the top of the variable are going to be processed are stored in the Operand stack , and if any exception occurs during method execution, then those exceptions are going to be stored the frame data.
- Stacks may be of fixed size or dynamic. It can be chosen independently during the stack creation.
Native Method Stacks
Native Method Stacks are the stack written in a different language other than the Java Programming language. These memory areas are allocated typically to each thread when threads are created. Size of this this memory area can be fixed or dynamic.
Program Counter (PC) registers
- It stores the address of the Java virtual machine instruction, which is currently executing.
- In Java, every thread has its own pc register.
- As a Java application contains some native libraries. If the method in the application is not native (i.e., written in Java) then the program counter contains the address of the JVM instruction currently being executed, and if it is native, then the PC register’s value is undefined.
Working of Garbage Collector
JVM uses this process to automatically allocate and deallocate the memory. Garbage Collector works in below three steps.
Marking-identifies what are the object not in use
Normal Deletion- Theidentified objects that are not in use has been removed from the heap.
Deletion and Compacting- Once the unused object is removed from the heap, the memory is being allocated automatically, these memory allocations are grouped together so that in future the memory allocation will be faster.
Compacting helps us grouping the memory together, so the allocation of new memory will be increased.
Types of Garbage Collector
Serial Garbage Collector- It works by holding all the application threads. It works with single threaded environments. It has a major drawback of freezing all the application threads during garbage collection. That is why it is not suitable for the server environment. We may use it only for simple command-line programs.
Parallel Garbage Collector-It is also called as the throughput collector. It is JVM’s default garbage collector. It uses multiple threads for garbage collection, but it also freezes all the application threads while performing garbage collection.
CMS Garbage Collector-CMS stands for Concurrent Mark Sweep. It uses multiple threads to scan the heap memory for the unused objects and then removes the marked instance. It holds the application threads while marking the referenced object in the tenured generation space and if there is any chance in heap memory in parallel while doing the garbage collection.
As compared to the parallel garbage collector, it uses more CPU to ensure better application throughput.
G1 Garbage Collector: It is used when there is a large heap memory area. G1 separates the heap memory into regions and group the unused memory area in parallel. It also compacts the free heap space on the go just after reclaiming the memory.
System.gc() and Runtime.gc() are the methods that request for garbage collection to the Java Virtual Machine explicitly.