Before we move on to the performence things, we need to learn that what is really going on in the background of JVM (Java Virtual Machine). This is the starting point of every developer who wants to learn and tune performance in order to gain some speed. So let’s dive into the world of codes.
In Java, memory management is handled by JVM automatically to store your variables, classes, fields and beyond… The first thing we will learn is in JVM, memory splitted into two regions. One of them is called Stack and the other one is Heap.
What is Stack?
First region we are going to learn is Stack. In JVM, stack is very efficent approach to memory management and not only just one but also every thread has its own stack region. In stack, instantiated fieds are added to memory one on another just like its name stacking. As you can see, this area is not big enough to store objects so what is happening is primitive types and object pointers can be stored directly instead of storing a whole object.
And when it is time to remove, first object needs to be removed first because like I said, data is stacked, we can’t reach the bottom unless we remove those items.
What is Heap?
Now, let’s look at the other region called Heap. As you can see it from the GIF, heap size is bigger than stack because heap is the main region for holding objects. Every created object is held in heap and its reference is helding in stack. Like figure shown below
public List<String> test() {
String newString = "test";
List<String> testList = new ArrayList<>();
testList.add(newString);
return testList;
}
On the contrary, there is just one heap that application has. That sounds quite reasonable because we might have more than one big object that passes one method to another. So basically, stack is used for local variables and there could be many stacks but they all use one heap to store the objetcs. Those objects pointers are held in stack so when we want to pass objects between methods, we are not copying objects like in stack, we are passing its reference.
Furthermore, heap is not one, solid region at all. If you zoom in to the heap, you will see 4 different areas.
Actually those are called generations. Heap is builded on two main generations. One of them is young and the other one is old. Young generation is divided into three spaces. Eden, survivor zero and survivor one spaces. It will be more clear when you learn what they do. Created objects are first placed in eden space. Then eden is fulled, objects are moved to the survivor one or survivor zero. After, created objects are placed in eden again. When eden is full, both eden and survivor zero or one will be moved to the survivor zero or one. If objects are moved more than five, those objects are now placed in old generation. This means, now those objects are needed and will live in old generation unless it looses its pointer. If there are no variables in the stack that holds its reference, this means that object is eligible for garbage collection. Last one is so important for performance issues and thus we need to learn how Java memory works to understand it.
One more room for Metaspace please!
Besides these regions, there is one more region in memory I want to mention. Metaspace is the region where application’s metadata is stored. Most of the times, we don’t need to know what is going on in metaspace due to its mission. And there is one more mission it has and that is holding static variables, methods and classes in it. This is why static keyword is accessable from anywhere because they are helding in metaspace so every thread can reach it easily.
Can we tune this size?
Absolutely. There are flags we can use that tells JVM what to do when we are starting the application. We can some of these flags
- -XmsNg to set initial size
- -XmxNg to set maximum size
- -XX:NewRatio=N ratio of the young generation to the old generation
- -XX:NewSize=N initial size of the young generariton
- -XX:MaxNewSize=N maximum size of the young generation
And we’ve reached our deadline. Before we move onto the Garbage Collection, it was essential to learn how Java memory works to have a better understanding about Garbage Collection. I hope I see you in my next article. Happy Coding!