1

Is there any possibility to implement a cache in memory to avoid full heap consumption?

My spring-boot java application uses cache in memory with an expiration policy set to 1 hour (Caffeine library is used for caching purposes). After that time all cache instances are in the old generation and require a full GC to be collected. Now with XMX set to 10GB, I can see that after few hours of tests my cache contains around 100k instances, but in heap (exactly in the old generation) I can find a few millions instances of cached objects. Is there any possibility to use cache in memory and avoid such situation?

Wenaro
  • 125
  • 8

4 Answers4

1

Problem which you described is call memory leaks.

Yes you Can, but it’s depends on which version GC you use. For example in G1 this problem should not appear. So, if that was possibile i recomend to you switch to G1.

XpauseTarget this flag is resposibility for avoid long pause in your system, so you Can split cleaning your heap to part.

Also you Can customize precent which demand run GC. -XX:InitiatingHeapOccupancyPercent=45

Damxx
  • 34
  • 3
  • By default CMS GC is used in that app, I will switch it to G1 and test that – Wenaro Mar 12 '21 at 10:02
  • Give me feedback Please – Damxx Mar 12 '21 at 10:26
  • G1 GC resolve the problem. I set Xmx to 5GB. With CMS GC I can see that after few hours up to 5GB of memory is used. But with G1 GC up to 2GB of memory is used – Wenaro Mar 15 '21 at 07:15
  • I’m happy to hear that. If you want, you might accept this answer for next, who want to find right answer. – Damxx Mar 16 '21 at 08:52
0

As you observed, caches and generational collectors have opposing goals. More modern collectors like G1 and Shenandoah are region-based which can allow them to better handle old gen collection. In the Shenandoah tech talks, you'll often hear their developers discussing an LRU cache as a stress test. This might not be a problem if your GC is well tuned.

You may be able to keep the cache data structures on heap, but move its entries off. That can be done by serializing the value to a ByteBuffer at the cost of access overhead. Another approach is offered by Apache Mnemonic which stores the object fields off-heap and transparently marshals the data. This avoids serialization costs but is invasive to the object model.

There are fully off-heap hash tables like Oak and caches like OHC. These move as much as possible outside of the GC, but there is a lot more overhead compared to an on-heap cache. This is comparable to using a remote cache, like memcached or redis, so that might be prefered. Memcached for instance uses slab allocation to very efficiently handle the memory churn.

Most often you'll see a small on-heap cache is used for fast local access of the most frequently used data that is backed by a large remote cache for everything else. If you truly need a multi-GB in-process cache, then off-heap might be necessary or you may have to tune your GC to accommodate this workload.

Ben Manes
  • 9,178
  • 3
  • 35
  • 39
0

Objects in the cache are always there if the expiration is not set. What you can do is tuning JVM to avoid that situation, i.e, if you are using CMS, -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly, with these two options being set, JVM is forced to do full gc while old generation is over 75%.

Foredoomed
  • 2,219
  • 2
  • 21
  • 39
0

Try Chronicle-Map

https://github.com/OpenHFT/Chronicle-Map

Chronicle Map is:

fast. Millions of operations per second, with low and stable microsecond latencies for reads and writes. Write queries scale well up to the number of hardware execution threads in the server. Read queries never block each other.

reliable. Chronicle Software have a “chaos monkey” test which verifies Chronicle Map multi-master replication in the face of node and network failures. The map can optionally be persisted to disk.

in production at banks and hedge funds, globally.

built using lessons learnt from real-world experience solving real-world problems.

open source (standard version), and in use at hundreds of sites around the world.