Last Updated: 18 Mar 2021 12:53 by ADMIN
Mvw Developer
Created on: 11 Mar 2021 14:37
Category: UI for ASP.NET Core
Type: Bug Report
Bug: memory leak when using ToDataSourceResult with Entity Framework core 2


Using any overload of ToDataSourceResult(Async) with a DataSourceRequest containing filter descriptors on an IQueryable provided by Entity Framework core 2.x, causes Entity Framework's "Query cache" to fill up for different filter values, leaking memory in the process. This is caused by using Expression.Constant in the generated expression trees for a DataSourceRequest's FilterDescriptors. Refer to https://github.com/dotnet/efcore/issues/14859 for more details about why this leaks memory.

Since Entity Framework's cache is always missed for new filter values, performance is also degraded due to this issue.

The issue is not present when using Entity Framework core 3 or greater. Apparently using Expression.Constant is not an issue on these version.

I have attached a minimal working example that illustrates the memory leak.


This issue can be fixed by changing FilterDescriptorExpressionBuilder.CreateValueExpression to not return a ConstantExpression, but a MemberExpression instead. Entity Framework 2.x will correctly parameterize the MemberExpression, causing the query cache to be hit correctly for queries that only differ in parameter values. It won't parameterize ConstantExpressions. Refer to https://stackoverflow.com/a/60516780/147993 for an example of generating a MemberExpression instead of a ConstantExpression.

Attached Files:
1 comment
Posted on: 18 Mar 2021 12:53


Thank you for bringing this issue to our attention. We've reviewed it and could confirm this is a valid bug report so I've modified its status to Unplanned in our Public Feedback portal: https://feedback.telerik.com/aspnet-core-ui/1510698-bug-memory-leak-when-using-todatasourceresult-with-entity-framework-core-2

This way other people could also see it and vote for raising its priority.
Based on the demand from the community we could plan and implement it in our future releases.

Best Regards,
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.