How to optimize Java gc for long live objects

My java application stores an internal cache that can grow up to 10 gigs. The policy expires 30 minutes or when the memory threshold is reached (I use local ehcache). Obviously, after 30 minutes, the entire cached object will be in the old gene, and it will take full gc to collect it. At the moment, the pause of a pause can reach 6 seconds, and I would like to reduce it.

The average size of an object is 500 thousand, but it can increase to 1 megabyte, so we are talking about 10000-20000 cached objects (actually byte arrays).

What is the best GC optimization strategy? I know that I can get a bunch, but this is a good decision of the last resort.

Thanks!

+6
source share
2 answers

I regularly work with caching services containing 10-30 gigabytes of data in the JVM heap. The Concordent Mark Sweep (GC) algorithm can handle these cases pretty well, with a Stop-the-World pause of about 100 ms (although the absolute numbers are hardware dependent).

You can find the GC tuning checklist for caching apps and heap sizes on my blog.

Here you can find more information about the Concurent Mark Sweep algorithm.

+2
source

A 10 GB cache is not something you should do on the heap. Use ByteBuffers for caching. Building an object does not have to be expensive. Thus, the GC is not involved, and you yourself can manage everything.

For example, if you implement the page cache in the Java database management system, you would not create objects for it, but would use byte buffers or managed byte buffers or the best byte buffers. You can learn more about these three here .

If you process more and then say a million objects at a time, you will see that the proportion of GC time is increasing. I saw situations when we managed to create a huge number of nodes for data processing, and it was very slow. Then we switched to the direct byte buffer scheme and even used some additional technique in which we could store more data (objects cost 24 bytes, at least each) and stopped thinking about objects in the first place. As a result, we processed data, not objects. This has increased performance many times, and we can process much more data than we expected.

After that, we noticed that all this corresponds to the database and it’s good, and that’s exactly where we put everything together.

So check out what direct buffers can do for you.

+3
source

Source: https://habr.com/ru/post/955434/


All Articles