Java Memory - Stack VS Heap - Java interview question

Why Java Momory is important:

  •     code optimization
  •     avoiding code errors
JVM - complecated
learn how memory is managed - java developer task

Stack/ Stack Memory:

  • A Stack is a Last In First Out (LIFO) data structure. It supports two basic operations called push and pop/pull.
  • The stack is a very efficient data structure which is managed effectively by the Java virtual machine. One important aspect of the stack is that Java knows exactly when data on the stack can be destroyed.
  • used: are great for local primitive variables (short lifetime)(like int, double) when do we use? 
  • Every thread has its own stack.  So, data on the stack can only be seen by the thread that owns the stack. 
  • a stack is used helps us understand how variable scope really works. 
  • memory size of a Java stack is generally much less than in a Java heap space because when a method ends, all the variables created on the stack are erased forever.
  • The size of the stack will vary as methods and functions create and delete local variables as needed.
  • Memory is allocated and then subsequently freed without you needing to manage the memory allocation.
  • Stack has size limits, which can vary according to the operating system you use.
  • Variables that are stored on the stack exist for as long as the function that created them are running.
  • suffer from size limitations and the fact that you cannot resize variables on the stack.
  • faster than heap
public class Main{
    public static void main(){
        int val = 1;
        val = update();
    }
    public static int update(int data){
        int tempVal = data + 5;
        int newVal = tempVal + 4;
        return newVal; 
    }
}


Heap/ Heap Space:

  • The secondary of Java's memory is called the heap
  • in Java, all objects (like strings, customers, or integer objects) are stored on the heap
  • allows us to store data that has a longer lifetime than a single code block or function, for example objects that need to be shared across multiple methods. 
  • heap isa huge area for storing data
  • In an application, there is one heap which is shared across all the threads and a number of stacks one for each thread.
  • For the objects on the heap there'll be a pointer to the object which is the variable reference stored on the stack.Memory is not managed automatically nor is it as tightly managed by the central processing unit the way stack is managed. You would need to free allocated memory yourself when these blocks are no longer needed.
  • The heap is prone to memory leaks, where memory is allocated to unused objects and will not be available to processes other than that.
  • There is no size limit in the heap.
  • Compared to stack, objects in the heap are much slower to access. It is also slower to write to the memory on the heap.

StackHeap 
ApplicationStack is used in parts, one at a time during execution of a threadThe entire application uses Heap space during runtime
Size limithas size limits (depending upon OS), smaller then Heapno size limit
Storeslocal primitive variables and references to objects that are created in Heap SpaceAll the newly created objects
OrderIt is accessed using Last-in First-out (LIFO) memory allocation systemThis memory is accessed via complex memory management techniques that include Young Generation, Old or Tenured Generation, and Permanent Generation.
LifeStack memory only exists as long as the current method is runningHeap space exists as long as the application runs
EfficiencyComparatively much faster to allocate when compared to heapSlower to allocate when compared to stack
Allocation/DeallocationThis Memory is automatically allocated and deallocated when a method is called and returned respectivelyHeap space is allocated when new objects are created and deallocated by Gargabe Collector when they are no longer referenced







Popular posts from this blog

GOOGLEFINANCE attributes

Practice

Merge/join two or more video files using Python