Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collector and performance hit in java

When i was testing application deployed in tomcat, the application was hanged. Side by side i was running visualvm. During application hang time what i saw in visualvm the garbage collection was going on. I have read in theory that when garbage collection starts working the system is halt as application threads stops during that period of time. I want to get clear idea on

How do other developer handle this situation?

How it can be handled in effective way without hitting the performance of the system?

Can it be considered as builtin inherent property Or there can be other effective solution?.

As, JVM can be configure with different garbage collector algorithm but in some instance of time it must run. If i am wrong in my understanding please correct me.

like image 576
abishkar bhattarai Avatar asked Jun 24 '26 13:06

abishkar bhattarai


2 Answers

Your application is hanging because GC is taking too long. I advise you to take an application performance profiling and try to understand why your application needs the GC to run so long. Look for unnecessary allocations, unused objects, collections that outgrow without any control.

I have run into this problem sometimes. The solution always involved some kind of optimization based on application performance profiling (I have used New Relic and AppDynamics for it in different occasions).

like image 63
Alexander Jardim Avatar answered Jun 26 '26 04:06

Alexander Jardim


There are two ways to approach this:

  • You can tune the JVM's garbage collection subsystem by selecting a "low pause" collector, and/or changing various tuning options. Unfortunately low-pause collection has a cost. You reduce the length of time that the system is paused while the GC runs, but the flip-side is that more CPU cycles are expended overall on the garbage collection and related things.

  • You can tune your application to reduce the amount of (long lived) garbage that it creates, and how much memory it leaks. However you need to be careful here too ... because some of the classic strategies (object pools, weak/soft reference based caches) can actually make things worse. At the very least you should profile first before attempting to reduce allocations.

There are other possible explanations too:

  • The "hangs" could be unrelated. They could be due to talking to slow external resources. They could be an unfortunate side-effect of threading bottlenecks.

  • The "hangs" could be cause by the system as a whole "thrashing". This happens when you try to use a heap that won't all fit into physical memory. The system then has to swap virtual memory pages between physical memory and disc ... on demand. Garbage collection (especially "full" GC) is likely to trigger this.

like image 39
Stephen C Avatar answered Jun 26 '26 02:06

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!