Automation QA Testing Course Content

STACK VS HEAP

DIFFERENCE  STACK VS HEAP


Stack Memory in Java

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 referred from the method that are in a heap.

Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects.

When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.

Key Features of Stack Memory

Some other features of stack memory include:

  • It grows and shrinks as new methods are called and returned, respectively.
  • Variables inside the stack exist only as long as the method that created them is running.
  • It’s automatically allocated and deallocated when the method finishes execution.
  • If this memory is full, Java throws java.lang.StackOverFlowError.
  • Access to this memory is fast when compared to heap memory.
  • This memory is threadsafe, as each thread operates in its own stack.

Heap Space in Java

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory.

These objects have global access and we can access them from anywhere in the application.

We can break this memory model down into smaller parts, called generations, which are:

  1. Young Generation – this is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up.
  2. Old or Tenured Generation – this is where long surviving objects are stored. 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.
  3. Permanent Generation – this consists of JVM metadata for the runtime classes and application methods.

Key Features of Java Heap MemorSome other features of heap space include:

  • It’s accessed via complex memory management techniques that include the Young Generation, Old or Tenured Generation, and Permanent Generation.
  • If heap space is full, Java throws java.lang.OutOfMemoryError.
  • Access to this memory is comparatively slower than stack memory
  • This memory, in contrast to stack, isn’t automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage.
  • Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code.

How does it work:

      Every time you create an instance of a class, some memory gets allocated on Heap to store that object and return a reference pointer to the start of that block of memory. This reference pointer comes in the form of a unique number represented in hexadecimal format, and as an integer it is stored on the Stack, so when we need to access that object on Heap, we find its reference on Stack which points to objects location on Heap, which is then accessed by that reference.

Example:





What is happening behind the wall(the scheme is showed below):

  1. When JVM find main() method, the Stack frame will be created. After we create an instance of class Example, which means, that memory will be allocated on Heap to store the object and its address will be stored on Stack in form of a pointer.
  2. When method fun1() is called, one more Stack frame will be created. local_var1 and its value will be stored on it, as it is local primitive variable. 
  3. Method fun2() is called. New Stack frame created and as with main() method memory allocation for object o will happen on Heap and pointer will be returned and saved on Stack.
  4. Method fun3() is called. Parameter obj is saved on Stack as it is pointer to an object on Heap, local_var2 and its value is saved on Stack as well. Memory allocation for string will happen on the Heap in String Pool in Java.
  5.  After all GC will be invoked and memory will be released.





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.