As a developer, it is extremely important to understand how memory management in java works. It can help avoid creating difficult to trace problems.
This blog post would give a simplistic view of memory management in java.
With respect to memory management of Java applications, you need to understand two terms, the stack and the heap
The memory of a Java application is divided into two sections, the STACK and the HEAP.
Stack is a last in first out data structure. The JVM knows exactly when the data on the stack can be destroyed.
All local variables are created on the stack, and they are automatically popped from the stack on encountering the closing braces of the block that created the variable.
As each thread has its own stack, the data on the stack can only be seen by the thread that owns the stack.
In an application you have a single heap, that is shared across all threads.
In java all objects are stored on the heap.
For an object on the heap, the pointer reference to it will be stored on the stack.
This blog post would give a simplistic view of memory management in java.
With respect to memory management of Java applications, you need to understand two terms, the stack and the heap
The memory of a Java application is divided into two sections, the STACK and the HEAP.
STACK
In a single Java application, there can be one or more than one stack(s). That is, each thread in Java has its own stack.Stack is a last in first out data structure. The JVM knows exactly when the data on the stack can be destroyed.
All local variables are created on the stack, and they are automatically popped from the stack on encountering the closing braces of the block that created the variable.
As each thread has its own stack, the data on the stack can only be seen by the thread that owns the stack.
HEAP
Heap allows us to store data that has a longer lifetime than a single code block.In an application you have a single heap, that is shared across all threads.
In java all objects are stored on the heap.
For an object on the heap, the pointer reference to it will be stored on the stack.