Search code examples
javacachingmemory-managementcaffeine-cache

Java memory cache


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?


Solution

  • 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