
Meaning, instead of going for more hardware or for more beefy machines you may want to look into how your memory is managed. The default Java garbage collection settings may not be perfect for your application, so to speak. There is also one more thing that you should consider when thinking about garbage collection performance tuning. They will give you detailed information regarding what’s happening inside your JVM, especially when it comes to heap memory and garbage collection. You can also use tools like jstat or any profiler. Knowing all of that we can judge if we are satisfied with how the garbage collection is working or if tuning is needed.Īnother thing you can look into is garbage collection logs that we discussed in the Understanding Java GC Logs blog post. If we would correlate that with the garbage collector timings we would see the whole picture. The largest portion of the memory, called the old generation, gets filled up and then is cleared by the garbage collector. Usually, it is a sign of a healthy JVM heap. In this chart, you can see something called “shark tooth”. For example, have a look at the following chart: It will provide you information regarding your JVM memory utilization, the garbage collector work and of course the overall performance of your application. So how do we say that the garbage collector does a good job? We can look into our monitoring, like our own Sematext Cloud.
Garbage collection code#
You will most likely be more effective in refactoring the code to be more efficient. If your JVM memory utilization looks good and your garbage collector works without causing trouble, you shouldn’t spend time turning your garbage collection. To be blunt, there are numerous situations where the way how the garbage collector works only highlights a bigger problem. Unless you are absolutely sure that the problem lies in the garbage collection, don’t start with changing JVM options. The first thing that you should know is that tuning the garbage collection should be one of the last operations you do. To avoid that we need to ensure that the garbage collector that is running for our JVM applications is well configured and is doing its job as good as it can. Your distributed systems can collapse because of elements not responding in a timely manner. This can lead to your users not being able to properly use your application at all. What’s dangerous, however, is a complete stop of the application threads for a very long period of time – like seconds or in extreme cases even minutes. We can expect threads to be stopped for very short periods of time. Meaning that 33% of the time the application was not doing its job - it was doing the housekeeping instead.

During our work as developers and consultants, we’ve seen situations where the garbage collector was working for 20 seconds during a 60-second window of time. This is why it’s crucial for the garbage collector to work as efficiently as possible. You can imagine that instead of handling the business logic of our application the CPU can be busy handling the removal of unused data from the heap. There are resources that need to be designated for the garbage collector so it can do its work. Why Is Garbage Collection Tuning Important?Ĭleaning our applications’ JVM process heap memory is not free. Everything depends on the situation and your needs. Or it can be as complicated as tuning all the advanced parameters to adjust the different heap regions. Which is by the way what you should start with. It can be as simple as adjusting the heap size – the -Xmx and -Xms parameters. Garbage Collection GC tuning is the process of adjusting the startup parameters of your JVM-based application to match the desired results. In languages like Java or Kotlin, we don’t need to take care of that – it is done automatically by the JVM, by its garbage collector.

In programming languages like C or C++, the cleaning of the memory is done by us, programmers, manually in the code. When it is no longer needed it needs to be removed from the memory to make room for new objects. When you create an object in your code it is assigned on a heap and stays there until it is referenced from the code. One of the most interesting benefits of running a JVM based application is automatic memory handling. You can move your applications from server to server, from operating system to operating system, without major effort or in rare cases with minor changes. In the majority of cases, you get interoperability between operating systems and various environments.

Especially when compared to languages like C/C++. Working with Java applications has a lot of benefits.
