Java Stack Memory
What is Java stack memory?
Stack memory is a physical space allocated in RAM. It will be used to load primitive values, references, method invocations and local variables. It follows FIFO (First In First Out) mechanism when managing items on stack. Which make sure that elements in the stack does not grow over the time.
Whenever a method gets called, new block (stack frame) is created in the stack memory to hold primitive values and reference to the other objects in the method. As soon as the method execution ends, block will be removed from the stack.
Stack memory characteristics,
- Size is very small compared to heap memory.
- Elements in stack memory cannot be accessible by the other thread.
- It is short lived memory.
- Faster than heap memory.
Practical example
Let’s try to understand stack memory by using real world example.
public class ClassicRoutine
{
public static void main(String[] args)
{
ClassicRoutine routine = new ClassicRoutine();
routine.goToShop();
}
private void goToShop()
{
goToShop();
}
}If you closely check on above code example you will notice that from the main method we are calling to gotToShop(). Then inside the gotToShop() we are calling back to gotToShop(). In technical term gotToShop() is a recursive function.
Get Athula’s stories in your inbox
Join Medium for free to get updates from this writer.
Now we will check how stack memory looks like with this example,
- Load the main method
- Load goToShop method
- Load goToShop method
- ………..
Since goToShop method is a recursive method it will continuously load goToShop method block to stack, but never get removed. As a result stack will filled with goToShop method block until it hit it’s maximum memory. Which means we should get an error if we run the program.
Let’s try to execute our example in IntelliJ idea.
Console output is shown in the above Figure 1. As we predict we end up with StackOverflow error. With that result we can make sure that our program get crashed by overflowing the stack memory. Only way to exceed the stack memory is, our goToShop method block get accumulated in the stack memory. So we can conclude that stack memory is used to load method blocks at runtime.
Let’s expand our example to explore this furthermore.
public class ClassicRoutine
{
int counter = 0;
public static void main(String[] args)
{
ClassicRoutine routine = new ClassicRoutine();
routine.goToShop();
}
private void goToShop()
{
System.out.println(counter++);
goToShop();
}
}Try to add counter to the goToShop method as above and check how many times it get called before it throws stack overflow error. In my case it is 10489. Means method block get loaded to stack for 10489 time without removing.
Then you might wonder what will happen if we remove the recursive call and execute goToShop method more than above number of times, and will it give stack overflow issue. Let’s try that.
public class ClassicRoutine
{
int counter = 0;
public static void main(String[] args)
{
ClassicRoutine routine = new ClassicRoutine();
for (int i = 0; i < 90000000; i++)
{
routine.goToShop();
}
}
private void goToShop()
{
System.out.println(counter++);
goToShop();
}
}Here we execute the method for 90000000 times.
But even for that much of iteration we are not getting stack overflow issue. Why?
Reason is in this case we are executing goToShop method one at a time and after completion of each execution it will be removed from the stack. So stack memory allocation will not be accumulated.
That’s it about stack memory and its practical usage.
Cheers.. 👍










