DEV Community

Cover image for Java Memory Management Demystified: Ensuring Stability and Efficiency πŸš€πŸ’‘
Sanjay R
Sanjay R

Posted on

Java Memory Management Demystified: Ensuring Stability and Efficiency πŸš€πŸ’‘

Introduction

Memory management in Java is crucial for efficient program execution. It ensures that your program uses memory wisely, automatically freeing up memory when it's no longer needed. This helps prevent memory-related issues like leaks, making your programs more dependable and stable.

lets take an example program,
Image description

Types of Memory

There are two memories: Stack and Heap. Stack follows (LIFO).

Stack Memory

In the example program, you can see the local variable inside the methods. These method names and their reference variables will be stored in the Stack, in LIFO (Last In, First Out) order. If one method is executed completely, it will automatically deallocate the memory in LIFO order.

Heap Memory

In the above program, there is an instance variable in the Test class. When creating the object using the new keyword, the instance variable will be copied and stored in the Heap Memory. The reference variable while creating the object will be in the Stack.

Image description

Hang on..., there is still one memory left: Metaspace.

Metaspace

Let's add a new variable inside the Test class.
static int age;
This is a static variable. Objects cannot hold static variables, so the static variable will be in the Metaspace. Static variable static methods will be stored in the Metaspace. Static has CMS (Common Memory Space), the static variable will be shared by all objects inside the heap.

Back to Heap Memory

Heap Memory is divided into multiple parts, the main thing is Young Generation in the Heap. The young generation is further divided into three: Eden Space, Survivor 1, Survivor 2. Objects that are created using the new keyword are stored in Eden. When Eden is full, objects are moved to the Survivors. There is one entity responsible for destroying these objects, its name is Garbage Collector.

Garbage Collector

The garbage collector destroys objects from the heap. When an object has no reference, the garbage collector will destroy it. When Eden Space is full, the garbage collector will intervene, moving objects to either Survivor Space 1 or Survivor Space 2. The Minor Garbage Collector will destroy only objects that have no reference variables.

back to MetaSpace

Metaspace is available in separate memory allocation. Before JDK 1.8, it was called Permanent Generation. After 1.8, it is called Metaspace.

Why it is changed?

Metaspace size is not fixed; it can scale the size when more variables are added. In Permanent Generation, the size is fixed. Metaspace is not a part of the Heap; it is allocated in the Native OS.

Image description

Conclusion

In conclusion, understanding memory management in Java is essential for writing reliable and efficient code. By managing memory effectively, you can create programs that run smoothly and avoid common pitfalls associated with memory usage. So, keep these principles in mind as you continue to develop your Java applications for optimal performance and reliability.

Top comments (0)