Sitemap

Java Stack Memory Over Heap Memory

4 min readJan 23, 2024

--

In this article we are going to compare Java Stack memory and Heap memory growth by make them to grow continuously. Interesting thing is that we will be executing same example by changing specific integer value and let Stack memory to grow at one time. And again, let Heap memory to grow next time.

Before going to compare Stack and Heap memory growth, you can refer following article to get some idea about Stack memory.

And you can refer this article to get more idea about Heap memory.

Ok… Let’s move on.

public class StackMemoryOrHeapMemory
{
private static List<int[]> list = null;

public static void main(String[] args)
{
try
{
list = new ArrayList<>();
callMe();
}
catch (OutOfMemoryError | StackOverflowError e)
{
System.out.println("List size " + list.size());
e.printStackTrace();
}
}

private static void callMe()
{
int[] array = new int[1024];
list.add(array);
callMe();
}
}

In the above example we will make Stack memory to grow as well as Heap memory to grow. Recursive call (in callMe method) helps us to grow the Stack memory. At the same time adding int array (int []) to ArrayList help us to grow the Heap memory.

So, lets execute the program and check the console output.

Press enter or click to view image in full size
Figure 1: Stack Overflow Error — Console output

According to Figure 1 we are getting StackOverflow error as Stack memory kept growing. Then how about Heap memory at the same time. Please note that in this case list size is 24759 according to the console output.

Press enter or click to view image in full size
Figure 2: Visual VM output — First execution

According to the Figure 2 when we are getting StackOverflow error, heap has grown up to 90MB only. Since 90MB is very small, Java can manage that Heap memory growth. But Stack memory growth is not.

In this case Stack memory growth beats the Heap memory growth.

  • Heap memory allocation for the int array

Memory allocated for int = 4 bytes

Get Athula’s stories in your inbox

Join Medium for free to get updates from this writer.

Memory allocated for 1024 size int array = 4 * 1024 = 4 KB

Memory allocated for int array for 24759 iterations = 4 * 24759 = 99,036KB = 96.7 MB.

Which is closer to what we got from Visual VM result. 😊

Let’s do some alternation in our program.

public class StackMemoryOrHeapMemory
{
private static List<int[]> list = null;

public static void main(String[] args)
{
try
{
list = new ArrayList<>();
callMe();
}
catch (OutOfMemoryError | StackOverflowError e)
{
System.out.println("List size " + list.size());
e.printStackTrace();
}
}

private static void callMe()
{
int[] array = new int[102400]; // Here array size increased.
list.add(array);
callMe();
}
}

Now we have increased the size of the array to 102400 in side the callMe(). Let’s check the console result.

Press enter or click to view image in full size
Figure 3: Out of Memory Error — Console output

Here when we are getting OutOfMemory error before the StackOverflow error. Please note that in this case list size is 7755 according to the console output.

Press enter or click to view image in full size
Figure 4: Visual VM output — Second execution

According to Figure 4 we are getting OutOfMemory error as heap has grown up to 3000MB . So Java can not manage that much of heap memory growth. Therefore it throws OutOfMemory error before we getting StackOverFlow error.

In this case Heap memory growth beats the Stack memory growth.

  • Heap memory allocation for the int array

Memory allocated for int = 4 bytes

Memory allocated for 1024 size int array = 4 * 102400 = 400 KB

Memory allocated for int array for 20728 iterations = 400 * 7755 = 3,102,000 KB = 3029.3 MB.

Which is closer to what we got from Visual VM result. 😊

So, we can conclude that in the second execution Heap memory grew faster than Stack memory. So, because of the after 7755 iterations, we end up with OutOfMemory error.

Hope you got the big picture we discussed in this article. Please do comment what do you think about this article. Any suggestions are welcome… 😊

See you on another interesting topic.

--

--