Skip to content

MemoryCache Evict Bug - Added item is evicted when capacity is hit #42624

Closed as not planned

Description

@xboxeer

Describe the bug

When we have a limit to the MemoryCache, its Evict logic does not behavior as expected
This is a bug we found in community Why do I need to call twice the Set on my size limited MemoryCache when I hit the size limit?, the replier of this thread helped build a reproduce sample

To Reproduce

the below code can reproduce this issue

class Program
    {
        private static MemoryCache _cache;
        private static MemoryCacheEntryOptions _options;

        static void Main(string[] args)
        {
            _cache = new MemoryCache(new MemoryCacheOptions
            {
                SizeLimit = 2
            });

            _options = new MemoryCacheEntryOptions
            {
                Size = 1
            };
            _options.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration
            {
                EvictionCallback = (key, value, reason, state) =>
                {
                    if (reason == EvictionReason.Capacity)
                    {
                        Console.WriteLine($"Evicting '{key}' for capacity");
                    }
                }
            });

            Console.WriteLine(TestCache("first"));
            Console.WriteLine(TestCache("second"));
            Console.WriteLine(TestCache("third")); // starts compaction

            Thread.Sleep(1000);

            Console.WriteLine(TestCache("third"));
            Console.WriteLine(TestCache("third")); // now from cache
        }

        private static object TestCache(string id)
        {
            if (_cache.TryGetValue(id, out var cachedEntry))
            {
                return cachedEntry;
            }

            _cache.Set(id, $"{id} - cached", _options);
            return id;
        }
    }

Expected result should be

first
second
Evicting 'first' for capacity
third
third - cached

The actual result is

first
second
third
Evicting 'third' for capacity
Evicting 'first' for capacity
third
third - cached

Exceptions (if any)

Further technical details

  • ASP.NET Core version
    3.1
  • Include the output of dotnet --info
    .NET Core SDK (reflecting any global.json):
    Version: 3.1.402
    Commit: 9b5de826fd

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.402\

Host (useful for support):
Version: 3.1.8
Commit: 9c1330dedd

.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.504 [C:\Program Files\dotnet\sdk]
2.1.505 [C:\Program Files\dotnet\sdk]
2.1.507 [C:\Program Files\dotnet\sdk]
2.1.508 [C:\Program Files\dotnet\sdk]
2.1.509 [C:\Program Files\dotnet\sdk]
2.1.511 [C:\Program Files\dotnet\sdk]
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.2.108 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.402 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.1-dev [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    VS 2019 16.7.3

Activity

  1. transferred this issue fromdotnet/aspnetcoreon Sep 23, 2020
  2. added
    untriaged
    on Sep 23, 2020
  3. added this to the 6.0.0 milestone on Oct 7, 2020
  4. removed
    untriaged
    on Oct 7, 2020
  5. modified the milestones: 6.0.0, Future on Jul 22, 2021
  6. added
    help wanted
    on Nov 15, 2023
  7. changed the title [-]MemoryCache Evict Bug [/-] [+]MemoryCache Evict Bug - Added item is evicted when capacity is hit[/+] on Sep 10, 2025
  8. rosebyte commented on Mar 11, 2026

    @rosebyte
    Member

    Triage: is seems a bug, we will take a look.

  9. self-assigned this
    on Aug 6, 2026
  10. rosebyte commented on Aug 21, 2026

    @rosebyte
    Member

    After an investigation, this is the intended behaviour of a size-limited MemoryCache. SizeLimit controls admission. If adding an entry would make the total size exceed the configured limit, that entry is not cached. Compaction is scheduled independently so that Set does not synchronously perform an expiration, priority, and least-recently-used scan of the cache.

    Cache entries are therefore best effort, and application correctness must not depend on a value being retained after Set. The current behaviour is also documented under “Limit cache size with SetSize, Size, and SizeLimit”.

  11. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-Cachingbughelp wanted

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions