Popular Feed

JAVA – Memory management and Allocation

Heap and Stack Memory in Java Program

public class Management {
               public static void main(String[] args) {//Line 1( main method or main thread)
                              int i=4;      // Line 2         
                              Object obj1 = new Object(); // Line 3  
                              Management manage = new Management (); // Line 4     
                              manage.foo(obj1); // Line 5
               } // Line 9
               private void foo(Object value) { // Line 6
                              String str = value.toString(); //// Line 7
                              System.out.println(str);
               } // Line 8
}
java heap stack memory

Line 1-when we run this program ,It’s create stack memory to be used by main() method thread and it loads all the Runtime classes into the Heap space.
Line 2- Primitive local variable stored into stack memory of main() method.
Line 3- we created in heap memory and stack memory contains the reference for it.
Line 4- In Line no 4 Manager object created.
Line 5- we call the foo() method. Top of the stack is created to be used by the foo() method.
Line 6- Top of the stack is created to be used by the foo() method.Java is pass-by-value, a new reference to Object is created in the foo() stack block.
·        Line 7- Created String, So now it goes in the string pool in the heap space and a reference is created in the foo() stack space for it.
Line 8- foo() method is terminated, So now foo() method in stack becomes free.
Line 9-  Free all the memory and execution of the program. because main() method terminates and the stack memory created for main() method is destroyed.
In above Program define Step by Step memory allocation

Now, let’s start about more Stack and Heap Memory

java heap memory

·        What is Heap Memory?

1.      It is created by the JVM (Java virtual machine) when its starts.
2.      Whenever an object is created, its must be stored in the Heap space.
3.      If heap space is full, Java throws java.lang.OutOfMemoryError.
4.      Heap has global access. That means all objects can be referenced from anywhere in the application.
5.      Heap memory is managedby two concepts till JAVA7

GarbageCollection

Works to free memory by clearing any by objects without any references in the methods. These are objects that are no longer being used. Clearing them ensures they don’t take up space in the Heap.
Young-generation, old-generation Java Heap Space into two generations.

Young Generation : It is place where lived for short period and divided into two

·        Eden Space (Heap) : When object created using new keyword memory allocated on this space.
Ex:    A a=new A()

·        Survivor Space (Heap) : This is the pool which contains objects which have survived after java garbage collection from Eden space.

Old Generation
The old generation will be holding those objects which survived after garbage collection from Young Generation.

·        Tenured Space(Heap) :The pool containing objects that have existed for some time in the survivor space.


In java 1.8 Permanent Generation(NON-Heap) in short we says (PermGen):
PermGen has been replaced with Metaspace since Java 8 release..Metaspace means re-sizes dynamically.

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application.

What is Stack Memory?

Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects that are in a heap, referred from the method.
Access to this memory is in Last-In-First-Out (LIFO) order. Whenever a new method is called, a new block on top of the stack is created which contains values specific to that method, like primitive variables and references to objects.
When the method finishes execution, it’s corresponding stack frame is flushed, the flow goes back to the calling method and space becomes available for the next method.


2 comments: