Home > Back-end >  Will Spring Cache cause memory leak issue?
Will Spring Cache cause memory leak issue?

Time:05-20

I am trying to use Spring Cache (@Cacheable annotation) on method level in the Spring Boot Application, but unlike other google guava cache, I have no idea if Spring Cache will cause a memory leak issue. Because it didn't have a size limitation or refresh policy, where and how long would the data be stored in the application? I assume it'd be memory, but will Spring itself clear it automatically? If not, when there might be millions of requests coming in hitting the application, will that trigger a memory leak issue?

My use case is that I have a heavy method per request, and I would like to only execute that method one time during my current request, after the request is done there is no need to keep the data in Cache, but how would I ensure my Spring Cache would be cleared after each request? I know there is a evict action, however, what if my request errors out before hitting my cache evict method so that it returns 500 directly, that means my last request data would always sit in the cache memory, with more and more requests like that which might cause a memory leak, correct?

CodePudding user response:

In Spring application, Perhaps you can disable the cache by :

spring.cache.type=NONE

The type of cache is by default automatically detected and configured. However you can specify which cache type to use by adding spring.cache.type to your configuration. To disable it set the value to NONE.

As you want to do it for a specific profile add it to that profiles application.properties in this case modify the application-dev.properties and add

spring.cache.type=NONE

This will disable caching.

This will disable caching.

CodePudding user response:

The Spring Framework's caching support and infrastructure, whether consumed through declarative annotation-based caching and demarcation of Spring application components (using either Spring Annotations, e.g. @Cacheable, or with JSR-107, JCache Annotations), or by using Spring's Cache API directly (not common), is simply an "abstraction", hence the Spring Cache Abstraction. There is NO underlying caching provider (implementation of this SPI), by default.

Of course, if you are using Spring Boot (on top of, or to consume the core Spring Framework), and you do not configure an explicit caching provider (see here), such as Redis, then by default, Spring Boot will configure and provide your Spring Boot application with a ConcurrentHashMap caching provider implementation (see here).

When the documentation states:

This is the default if no caching library is present in your application.

It means when no caching library, like Redis (using Spring Data Redis, for instance), is detected on your Spring Boot application classpath.

In general, however, it is a good practice to choose an underlying caching provider implementation, such as Redis, or in your case, Google Guava, which is possible to position as a caching provider implementation in Spring's Cache Abstraction (see here, for example).

Given the Spring Framework's Cache Abstraction is simply a Facade with an caching API/SPI common to multiple caching provider implementations, effectively providing the lowest common denominator for caching functionality (e.g. put, get, evict, invalidate) across caching providers, then to your question, there is no cause of memory leak originating from Spring's "Cache", which really is not even a thing anyway. It is technically the provider implementation's "cache", like Google Guave, Redis, Hazelcast, Apache Geode (VMW GemFire), etc, that would actually be the cause of a memory leak if a leak existed in your application in the first place.

In other words, if there is any memory leak, then it originates with the caching provider.

You should refer to your caching provider's documentation on configuring critical aspects of the cache, such as memory management, that are explicitly stated to be beyond the control of the Spring Framework's Cache Abstraction.

The reason these aspects are beyond the control of the core Spring Framework is simply because the configuration of these low-level cache features (e.g. memory management) is usually very provider specific and varies widely from 1 provider to the next, especially with respect to the capabilities and features.

I hopes this explanation gives you clarity on the position of the Cache Abstraction provided by Spring and its responsibilities.

By adhering to the abstraction then it effectively makes it easier to switch between caching providers if your application requirements, UC or SLAs change.

CodePudding user response:

You can still specify what cache you want. For instance you can use Caffeine which

"... provides an in-memory cache using a Google Guava inspired API"

And you can still configure it for instance for a maximumSize

spring:
    cache:
        cache-names: instruments, directory
        caffeine:
            spec: maximumSize=500, expireAfterAccess=30s

To enable it you just have to add the dependency and add the @EnableCaching in a config class (and add @Cacheable of course in your method)

See:

  • Related