Java Memory Allocation — Stack and Heap

Nemanja Žunić
Apr 27, 2018 · 4 min read

One of the things JVM is responsible for is memory management.

Say you create a variable or an object in a Java app. Where is it stored? How long does it ‘last’? When is it removed from the memory?

Java Stack Space

JVM manages a Stack structure in which it stores local variables used in functions during their execution.

Let’s say we have a simple app that looks like this:

and we want to execute this code.

Initially, JVM initializes an empty Stack:

When JVM starts a function execution, it will create a frame within the Stack for that function. Since main function is executed when the app starts, the frame called main will be created in the Stack:

On line 15 we have another function execution:foo(); so JVM will create a new frame within the Stack for function foo:

And within that frame JVM will store all the local variables used in the function foo— in our example variable numb will be stored:

Next, JVM will execute line 7, another function call: bar(numb);

Again, a new frame will be created for function bar, and all local variables will be stored within, arguments included!

When function bar ends its execution, its frame will be removed from the Stack, along with all of the local variables within:

Same goes with function foo:

Java Heap Space

But that’s not the whole story. Along with Stack, JVM manages Heap storage. Heap is where all the objects in our app are stored. And we access those objects by storing references to those objects in Stack storage.

Let’s look at another example:

Same as before, initially we will have an empty Stack. But along with it, we have an empty Heap as well:

We know that JVM creates a frame in Stack for each function and puts local variables within, so let’s fast-forward execution to line 8 in App.java:

On line 8: Person p1 = new Person(25, 185);we have a local variable p1, which will be stored on a Stack. And it points to an object Person which will be stored on Heap along with its attributes and their values:

Same goes for line 9: Person p2 = new Person(75, 200);:

When function main ends, its frame and all of its local variables are removed from the Stack:

That’s when the part of JVM called Garbage Collector kicks in and removes all the objects stored in Heap that are not referenced by any of the variables in the Stack:

Conclusion

This concludes my second technical post/tutorial on Java. Hope you learned something new. Feel free to leave a comment or a suggestion and stay tuned for more 😄

Java Vault

Collecton of Java and CS knowledge.

Nemanja Žunić

Written by

I write sentences that make the magic happen (software developer, basically the same thing).

Java Vault

Collecton of Java and CS knowledge.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade