Skip to content

EFCore based Memory Leak #450

Closed
Closed
@Poltuu

Description

@Poltuu

Description

There is a memory leak, based on the way EF Core cache queries, especially queries with parameters automatically generated with such a librairy.

The following test (on GetTests.cs) shows just that, with cachedQueryCount being equal to 1000 instead of 1.

        [Fact]
        public async Task Memory_Leak()
        {
            for (int i = 0; i < 1000; i++)
            {
                await _fixture.SendAsync("GET", $"/api/v1/courses?filter[number]:eq={i}", null);
            }

            int cachedQueryCount = 0;

            var cache = _fixture.Server.GetService<IMemoryCache>() as MemoryCache;
            var entriesProperty = typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);
            var entries = entriesProperty.GetValue(cache) as ICollection;
            var items = new List<string>();
            if (entries != null)
            {
                foreach (var item in entries)
                {
                    var methodInfoVal = item.GetType().GetProperty("Value");
                    var val = methodInfoVal.GetValue(item) as ICacheEntry;
                    if (val?.Value == null)
                    {
                        continue;
                    }
                    var contentType = val.Value.GetType();
                    var gens = contentType.GenericTypeArguments;
                    if (gens.Length == 2 && gens[0] == typeof(QueryContext) && gens[1] == typeof(IAsyncEnumerable<CourseEntity>))
                    {
                        cachedQueryCount++;
                    }
                }
            }

            Assert.Equal(1, cachedQueryCount); //Except it's 1000
        }

Environment

  • JsonApiDotNetCore Version: master

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions