Stacks and Heaps

Code example:

class A {
int field; (heap)
void method() { (stack when called)
   Function<Integer, Integer> func = x -> field + x; (lambda in heap)
  }


x is not stored anywhere 

A is a class so the instance field field would be on the heap.func is a local variable, so it would go on the stack.func refers to the lambda expression, which is internally implemented as an
anonymous class, so it refers to an object on the heap.
Finally, x is an argument to the lambda expression, so it is not stored anywhere  


Heap:
- Calling a new ArrayList()
- Garbage variable 
- New variable

Stack:
- Static variables
- Method Calls
- Local variables within method calls during ex stack are mostly for method

Heap:
Heap is used to allocating memory to object. When creating an object, it will create in the Heap Space. Any object created in here has global access and can be referenced anywhere of the application
Objects created in heap references to objects that are stored in stack memory
> Access is slower compared to stack
> Needs Garbage collector to free up unused objects
> not thread safe and needs to be guarded by synchronizing code. 
Garbage collector goes through objects in the heap and removes those who dont have any references in the program.
However, we cannot predict when the objects will be garbage collected.
Stack:
Used during execution. Contain method specific values that are short-lived and reference other objects in the heap. The stack is LIFO.
Used for static memory allocation and thread execution
> when methods are called and return, it grow and shrink respectively
> Automatic allocate and deallocate when method finishes execution
>  Access if fast compared to heap
> Memory is thread safe


Differences:
  1. Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.
  2. Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
  3. Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.
  4. Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc, more details at Java Garbage Collection.
  5. Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.
  6. We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.
  7. When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.
  8. Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.
Step by Step:
> When Main() is called, space in the stack is created to store primitive and references to any object
> references variables that point to an object, the object itself is stored in the heap
> When constructor of the object is called, will allocate memory on top of the stack and store

    • object(this) reference on the stack
    • primitive values parsed into the constuctor
>  Any object that is the same type as one that is called before will have its instance variables stored in heap memory


<Prev Exceptions and Binding                        Next Parallel, Fork join framework>

No comments:

Post a Comment