Memory Management in Stack & Heap in Java

Tram Ho

Heaps and Stacks

  1. Heap:The heap is where Java objects are stored. When creating an object using the “new” keyword, the object is stored in the Heap. The heap can exist until there are no more references to the object or until the program terminates.
  2. Stacks:Stack is where local variables and method parameters are stored. Each time a method is called, a new frame is created on the stack to store the local variables and parameters of that method. When the equation is complete, the frame is removed from the stack and the memory is freed.

Note:

  • A frame (also known as an activation record or call stack frame) is a unit of data that is created when a method is called in a Java program. Each frame contains information about local variables, method parameters, and return values.
  • Each time a method is called, a new frame is created on the top of the stack, and the method parameters and local variables are allocated on this frame. Values ​​passed into the method are passed into the frame via the frame’s parameter.
  • When the method completes, the frame is removed from the top of the stack and the memory is freed. Method return values ​​are passed back through the previous frame in the Stack.
  1. How to store memory on Heap and Stack differently:
  • In the Heap, memory is dynamically allocated by the JVM when objects are created, and memory is released when there are no more references pointing to that object.
  • In the Stack, memory is allocated statically when a method is called and the memory is released when the method completes.

For example

In the example above:

  • The global variable global1 is stored on the Heap, more specifically in the static memory(data segment). When the class is loaded into memory, this global variable will be initialized.
  • The global variable global2 is stored on the Heap, but in non-static memory, where all instance variables of a class are stored. It is created when an instance is created.
  • The main method args parameters are stored on the stack because they are method local variables.
  • The local variable localVar is stored on the stack because it is also a method local variable.
  • The object obj is initialized with the new keyword, so it will be stored on the heap and a reference to the object will be stored on the stack.

Note:

  • When the ” main ” method is called, a new frame is created on the Stack to store the local variables and method parameters including localVar and the args parameter. Then a new object is created on the Heap using the keyword ” new “, and the address of this object is assigned to the variable obj . when System.out.println(obj) the address of the object is printed to the screen.
  • When the ” main ” method terminates, the frame on the Stack is removed from the Stack and the local variables and method parameters are released from memory. However, the object created on the Heap will continue to exist until it is picked up by the garbage collector.

(At the moment when I write this article, I’m just an ordinary student. So maybe the knowledge I found out above may be wrong or right. In the worst case, it’s wrong, I hope everyone has some understanding. You can help me by commenting below so I can find out again, but I don’t pray for bricks and stones to build a house, so I hope everyone reminds me!

Good luck and thank you everybody!

Share the news now

Source : Viblo