Stack vs Heap - Java
STACK Stack is a memory space , reserved by operating system for our process. Size of stack memory is fixed, it is determined in the compiler phase based on variables declaration and other compiler options. When memory is full, then java runtime throws java.lang.StackOverflowError We can use –Xss to define the size of stack memory. Method invocation and local variables lives in stack memory Stack memory used for execution of a thread Remember that Stack memory is always referenced in LIFO (Last-In-First-Out) order, It does means when we call a method, it will be on top of call stack. Method stack exists only till life of method, from the calling until the return. Local variables are also known as slack variables , because they are on the method stack, they exist as long as the method is executed. If the local variable is a reference to an object, only the variable goes on the stack. main method is a special method (the process starts and ends with main) its loc