Skip to content

Commit

Permalink
Add repro for dotnet/efcore#13048.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Chandler committed May 16, 2019
1 parent 0d65ccc commit 2562438
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Linq2SqlEFCoreBehaviorsTest.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props" Condition="Exists('packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="packages\xunit.core.2.4.1\build\xunit.core.props" Condition="Exists('packages\xunit.core.2.4.1\build\xunit.core.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C14AFEDC-21FF-4D96-ACFE-4E858D3CFAA3}</ProjectGuid>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>Linq2SqlEFCoreBehaviorsTest</RootNamespace>
<AssemblyName>Linq2SqlEFCoreBehaviorsTest</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
Expand Down Expand Up @@ -35,7 +36,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
<StartupObject>MemCompare.ProgramLeakTest</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dapper, Version=1.50.5.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -158,6 +159,7 @@
<Compile Include="ClientSideEvalTests\CountTests.cs" />
<Compile Include="ClientSideEvalTests\ToDictionaryTests.cs" />
<Compile Include="ClientSideEvalTests\WhereTests.cs" />
<Compile Include="MemoryLeakTests\MemLeak.cs" />
<Compile Include="MiscTests\NullableTypeInConstructor.cs" />
<Compile Include="MiscTests\QueryableInSelectTest.cs" />
<Compile Include="MiscTests\IntermediateProjection.cs" />
Expand Down Expand Up @@ -221,6 +223,7 @@
</PropertyGroup>
<Error Condition="!Exists('packages\xunit.core.2.4.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\xunit.core.2.4.1\build\xunit.core.props'))" />
<Error Condition="!Exists('packages\xunit.core.2.4.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\xunit.core.2.4.1\build\xunit.core.targets'))" />
<Error Condition="!Exists('packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<Import Project="packages\xunit.core.2.4.1\build\xunit.core.targets" Condition="Exists('packages\xunit.core.2.4.1\build\xunit.core.targets')" />
</Project>
119 changes: 119 additions & 0 deletions MemoryLeakTests/MemLeak.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Linq2SqlEFCoreBehaviorsTest;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;

namespace MemCompare
{
class ProgramLeakTest
{
static void Main(string[] args)
{
//L2S();
EFCore();
}

private static void EFCore()
{
using (var databaseFixure = new DatabaseFixture())
{
Console.WriteLine("Start!");

var builder = new DbContextOptionsBuilder<Linq2SqlEFCoreBehaviorsTest.EFCore.EFCoreDataContext>();
builder.UseSqlCe(databaseFixure.Connection);

var options = builder.Options;

for (int i = 0; i < 10000; i++)
{
using (var context = new Linq2SqlEFCoreBehaviorsTest.EFCore.EFCoreDataContext(options))
{
Console.WriteLine(i);
var data = new LeakTest().EFCore_ProjectionWithInstanceMethodCall(context);
}
}

GC.Collect();
Console.WriteLine("Done!");
}
}

private static void L2S()
{
using (var databaseFixure = new DatabaseFixture())
{
Console.WriteLine("Start!");

for (int i = 0; i < 10000; i++)
{
using (var context = new Linq2SqlEFCoreBehaviorsTest.Linq2Sql.Linq2SqlDataContext(databaseFixure.Connection))
{
Console.WriteLine(i);
var data = new LeakTest().L2S_ProjectionWithInstanceMethodCall(context);
//var data = new LeakTest().L2S_CompiledQueryProjectionWithInstanceMethodCall(context).ToList();
}
}

GC.Collect();
Console.WriteLine("Done!");
}
}
}

public class LeakTest
{
public LeakTest()
{
// NOTE: Does not leak.
L2S_CompiledQueryProjectionWithInstanceMethodCall =
CompiledQuery.Compile((Linq2SqlEFCoreBehaviorsTest.Linq2Sql.Linq2SqlDataContext context) => context.Employees.Select(w => new Projection { Name = MyFunc(w.Name) }));
}

// NOTE: This leaks ~35MB+ in 10,000 executions.
public IEnumerable<Projection> EFCore_ProjectionWithInstanceMethodCall(Linq2SqlEFCoreBehaviorsTest.EFCore.EFCoreDataContext context)
{
return context.Set<Linq2SqlEFCoreBehaviorsTest.EFCore.Employee>().Select(w => new Projection
{
Name = MyFunc(w.Name)
}).ToList();
}

// NOTE: Does not leak.
public IEnumerable<Projection> EFCore_ProjectionWithLocalLambda(Linq2SqlEFCoreBehaviorsTest.EFCore.EFCoreDataContext context)
{
Func<string, string> myFunc = name => name;

return context.Set<Linq2SqlEFCoreBehaviorsTest.EFCore.Employee>().Select(w => new Projection
{
Name = myFunc(w.Name)
});
}

// NOTE: Does not leak.
public IEnumerable<Projection> L2S_ProjectionWithInstanceMethodCall(Linq2SqlEFCoreBehaviorsTest.Linq2Sql.Linq2SqlDataContext context)
{
return context.Employees.Select(w => new Projection
{
Name = MyFunc(w.Name)
});
}

public Func<Linq2SqlEFCoreBehaviorsTest.Linq2Sql.Linq2SqlDataContext, IQueryable<Projection>> L2S_CompiledQueryProjectionWithInstanceMethodCall;

private string MyFunc(string value)
{
return value.ToString();
}
}

public class Projection
{
public Projection()
{
}

public string Name { get; set; }
}
}
Binary file modified TestDb.sdf
Binary file not shown.
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
<package id="xunit.core" version="2.4.1" targetFramework="net462" />
<package id="xunit.extensibility.core" version="2.4.1" targetFramework="net462" />
<package id="xunit.extensibility.execution" version="2.4.1" targetFramework="net462" />
<package id="xunit.runner.visualstudio" version="2.4.1" targetFramework="net462" developmentDependency="true" />
</packages>

0 comments on commit 2562438

Please sign in to comment.