Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostEvictionCallBack causing memory leak #191

Open
kcritchnau-qt opened this issue Oct 3, 2023 · 0 comments
Open

PostEvictionCallBack causing memory leak #191

kcritchnau-qt opened this issue Oct 3, 2023 · 0 comments
Labels

Comments

@kcritchnau-qt
Copy link

kcritchnau-qt commented Oct 3, 2023

Describe the bug
I am using the RegisterPostEvictionCallback to auto-refresh my cache when it expires. This is causing my memory usage to gradually creep up over time. I am logging when the eviction happens, as well as when the GetOrAdd() is being called, and both are firing. I suspect it's an issue with garbage collection.

Here's a graph of the memory usage today. I had a failed deployment at 10:20 that took the site down for a while, as you can see once it came back up the memory usage has crept up consistently.

image

Here is the code I am using to configure the cache:

private MemoryCacheEntryOptions GetCacheEntryOptions()
      {
          var options = new LazyCacheEntryOptions().SetAbsoluteExpiration(_cacheOptions.Timeout,
              ExpirationMode.ImmediateEviction);
          
          options.RegisterPostEvictionCallback((keyEvicted, value, reason, state) =>
          {
              if (reason is not (EvictionReason.Expired or EvictionReason.TokenExpired)) return;
              var keyString = keyEvicted.ToString();
              _logger.LogInformation("Evicted division {KeyString} from cache", keyString);
              var division = int.Parse(keyString);
              _appCache.GetOrAdd(keyEvicted.ToString(), () => GetDivisionUsersForCache(division),
                  GetCacheEntryOptions());
          });
          return options;
      }

Expected behavior
The memory usage stays consistent.

** Framework and Platform

  • OS: Windows Server 2019
  • Framework: .NET 6
  • LazyCache.AspNetCore 2.4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant