exampracticeonline.com
Stack and Heap

Introduction Java Memory

To run an application in Java, Java Virtual Machine(JVM) divides memory into stack and heap memory.

In Java, Whenever we declare new global or local variables, objects, methods, call a method or perform similar operations, JVM designates memory to these operations from either Heap Memory or Stack Memory.

What Is Java Stack Memory?

A Java stack memory is part of your computer’s memory where temporary variables are stored of all invoked methods that you have created.
Stack Memory in Java is used for static memory allocation and the execution of a thread.

When a new method is invoked, a new block of temporary memory will be created in the Stack in LIFO order.

This new block will store the temporary values and local variables invoked by the method and references to objects stored in the Heap that are being used by the method.After the method is finished, the memory containing those values is cleared to make free memory for new methods.

Key Features of Stack Memory

Below are some other features of stack memory:

  • This is the temporary memory where variable values are stored when their methods are invoked.
  • It uses LIFO(last in first out) data structure.
  • This LIFO(last in first out) data structure makes it easy to find the values needed and allows fast access to those values.
  • If this memory is full then Java throws java.lang.StackOverFlowError.
  • It’s automatically allocated and deallocated when method finishes execution.
  • This memory is threadsafe as each thread operates in its own stack.
  • When the method finishes execution, it’s corresponding stack frame is flushed.
  • It is used to execute a thread and may have certain short-lived values as well as references to other objects.

What Is Java Heap Space?

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes.

Whenever we create any new object, it’s always created in the Heap space.It is created when the program is run, and its memory size may decrease or increase as per object available in program.

When it become full, garbage collection(gc) is initiated for cleanup process. This is when objects that are no longer used are deleted to make free space for new objects.

Generation Types of Java Heap Space

Below are types of heap space:

  • Young Generation – All new objects are allocated and aged first into Young Generation.
  • Old or Tenured Generation – All long surviving objects are stored in Old or Tenured Generation.When objects are stored in the Young Generation, a threshold for the object’s age is set and when that threshold is reached, the object is moved to the old generation.
  • Permanent Generation – This consists of JVM metadata for the runtime classes and application methods.

Key Features of Java Heap Memory

Below are some other features of heap space:

  • Heap space in Java is used for dynamic memory allocation for Java objects and JRE classes at the runtime.
  • New objects are always created in heap space and the references to this objects are stored in stack memory.
  • If heap space is full, Java throws java.lang.OutOfMemoryError.
  • Access to this memory is relatively slower than stack memory.
  • It is managed by two concepts: Garbage collection and young-generation, old-generation.
  • Garbage collection works to free memory by clearing any by objects without any references in the methods.
  • Young-generation, old-generation helps prioritize objects for garbage collection by dividing Java Heap Space into two generations.
  • Heap isn’t threadsafe and needs to be guarded by properly synchronization.

Difference between Java Heap Space and Stack Memory?

Stack Memory Heap Space
Primitive local variables are only accessed the Stack Memory blocks that contain their methods. Objects stored in the Heap can be accessed throughout the application.
Stack contains any reference to those objects. Heap Space contains all objects are created.
Stack is accessed through a last-in, first-out (LIFO) memory allocation system. Memory allocation in the Heap Space is accessed through a complex, young-generation, old-generation system.
Stack memory exists as long as the method runs.After finished method Stack memory clean for next storage of new method. Heap Space exists as long as the application runs and is larger than Stack, which is temporary.
Stack memory works fast because it is using (LIFO) memory allocation system. Heap Space works slow as compare to Stack memory because it is using a complex, young-generation, old-generation system.
Stack has size limits depending upon type of OS and is usually smaller then Heap. Heap has no size limit.
This Memory is automatically allocated and deallocated when a method is called and returned respectively. Heap space is allocated when new objects are created and deallocated by Gargabe Collector when they are no longer referenced