SAP Java Administration – Troubleshooting part II – understanding garbage collection basics
Hello SDN Community
In part II of the blog series I will write about the basics of garbage collection which is an important topic for an administrator to understand for Java based SAP systems.
It can also be interesting for functional persons who use the Java based SAP systems as it can explain why crash situations occur or why performance issues occur.
If you haven’t read part I please do so before continuing, I will be using a similar example as the one I sued in part I to explain the basics of garbage collection. You should be familiar with what a java object represents and know that java reuses and recycles which is what part I of the blog series handles.
In both ABAP and Java you have limits (in the form of parameters) that define the boundaries of how much memory can be used. The way memory is allocated and handled in Java however is different compared to ABAP.
Java memory area’s
The sum of young generation and old generation is referred to as the Heap Space (everything that is inside the black square is a part of the heap space).
The young generation space consists out of eden and survivor space 1 and survivor space 2. The tenured space is also referred to as old generation space (see picture 1.1).
The red Audi R8 from part I would reside in the heap space as would the other cars created.
Another space that exists is the permanent space (see picture 1.2), it holds methods and classes that are loaded when an application is launched. The class Car and the constructor method of the class Car from part I would reside in the permanent space when the race application is loaded.
Garbage collection in java is strongly related to memory use.
Minor garbage collection
As an example this time we will use a qualification race application (drivers get x number of laps to get the best possible time).
When the application is launched you already saw in part I, objects are created. At the moment the Red Audi R8 is created, a java object which requires memory.
When an object is created, it is considered alive. Such a java object of the class Car is created in eden (see picture 2.1). The space is called eden because the objects that are in there are young (translated to the example application this means a car that just entered the race).
The heap space and the individual spaces are given an initial size and a maximum size through parameters; those sizes are set at startup of the Java engine. The survivor area sizes are based upon a parameter setting which determines the size of the survivor spaces compared to the young generation space size, for example, each survivor space as 1/8 of the size of the young generation space
For example we are in lap 1 of race. Now let’s assume for example that the eden space (only the blue square) is 60MB in size (see picture 3.1). We will assume a car has a size of 4MB.
I’m going to keep it simple to show how the mechanism works and to explain the different terms, in a real situation you will not have the full 60MB available as your Java engine already launches a number of applications and has periodic jobs running, you will already have an amount of memory that is already used in the eden space.
Additional cars such as the BMW M5 that enter the qualification race are also created in the eden space (see picture 3.1).
The eden space can get 15 cars allocated in terms of memory before it’s full. 4MB for one car * 15 = 60MB. Once you want to create the 16th car (for example a Nissan 380Z), an allocation failure takes place. The application wants to allocate 4MB but it is not available.
What happens once the allocation failure takes place is that garbage collection is triggered. The garbage collection that takes place when an allocation failure of eden occurs is called minor garbage collection. A minor garbage collection is performed very fast and the end-user doesn’t notice that it occurs. A minor garbage collection scans the young generation space.
Now let’s assume the BMW M5 and the 13 other cars finished their laps to set a qualification time and left the race track.
This would mean the object which represents the BMW M5 is marked for garbage collection. When the garbage collection runs takes place, the object is destroyed as it is no longer necessary. The Audi R8 still has some laps he can try and set the best time so the object is still alive and is moved to one of the survivor spaces (moved into survivor space 1 in this example). This mechanism frees up memory in the eden space and the Nissan 380Z is created. (see picture 4.1)
A promotion is when an object lives long enough to be promoted from the young generation space into the tenured space. Translated into our example, if a car is already driving on the track for many laps, he will be promoted to the tenured space (the object is already alive for a long time).
How long objects have to survive to get promoted depends on multiple factors. You can have parameter settings that determine how often an object has to move between the survivor spaces, you can have a promotion because the survivor space fills up which causes an allocation failure, it can even happen that an object is moved directly from the eden space to the tenured space if the size of the object does not fit into the survivor space and so on.
Let’s assume more cars are created (enter the race track to set qualification time) and a minor garbage collection occurs. What happens is, the Nissan 380Z will get moved into a survivor space if he still has laps left to set a qualification time, the Audi R8, given he also still has laps left to set a qualification time, is moved into the other survivor space (the survivor space in which the Audi R8 was not in previously).(see picture 5.1)
The objects that stay alive are moved back and forth in the two survivor spaces until the limit is reached that they considered old enough (as previously mentioned this can be caused by different events).
Let’s assume a garbage collection takes place and that the Audi R8 has been on the track long enough and get’s promoted; it means the object will be moved from the survivor space into the tenured space. The Nissan 380Z isn’t yet old enough and is moved from one survivor space to the other. (see picture 5.2)
Full garbage collection
Now let’s take a look at the tenured space. We assume it’s 260MB in size in our example (see picture 6.1). This means it can hold (260MB / 4MB) 65 cars in terms of memory allocation. To have 65 cars in the tenured space, they would have to be on the track for many laps already. Now what happens when another car is also on the track long enough to be promoted into the tenured space, an allocation failure takes place, the application doesn’t have the free memory in the tenured space to place the object in it (the car that is on the track long enough).
An allocation failure of the tenured space causes a full garbage collection to take place. A full garbage collection is considered a “stop the world” event and the end-user gets an hourglass icon and has to wait for the garbage collection to be finished before the application can be continued. A full garbage collection scans the whole heap size and therefore also takes noticeably longer than a minor garbage collection. The larger the heap size is, the longer it takes to scan it.
The process itself is the same as for to the minor garbage collection, objects that are marked for garbage collection are destroyed and memory is freed up.
When none of the 65 cars that consume the tenured space (100% allocated) is marked for garbage collection (thus all cars may still do laps to set the qualification time), an outofmemory situation occurs. The application cannot move the 66th car into the tenured space as the memory that needs to be allocated is not available and throws an outofmemory error which often lead to a crash of the java virtual machine or an automatic restart of the java virtual machine.
There are of course other possible crash situations but I will write more about it in upcoming parts. This part is meant to give you a basic understanding how the mechanism of garbage collection works and what the different terms stand for.