I've been noticing that my web application is leaking memory somewhere. The leak is very slow, climbing about 1% every day on the server. In an effort try and find out what is causing these memory leaks I pulled dump files to compare about 4 days apart.
Comparing those two dump files shows the following:

All of these Microsoft.CodeAnalysis.CSharp.InternalSyntax.* instances add up to about 185mb of memory increase over those 4 days.
Viewing the instances of them looks like it is coming from my Razor mail templates.

Any idea why this is happening?
Here is my implementation of RazorLight for these templates:
Is there something wrong with my implementation here or is there a bug somewhere in RazorLight? Thanks!
I created a simple load test to run 10,000 iterations of parsing embedded resource templates:
```c#
var engine = EngineFactory.CreateEmbedded(typeof(Program));
for(int i = 0; i < 10000; i++)
{
var model = new BaseMailModel
{
WebVaultUrl = "http://google.com",
SiteName = "Google"
};
var htmlContent = engine.Parse("Templates.Welcome", model);
var textContent = engine.Parse("Templates.Welcome.text", model);
}
```
Sample project for this test: RazorLightMemoryTests.zip
Diagnostic tools shows the same results as my productions dumps. There is definitely something leaking memory in here.

Look at all those GC.
The issue is not present in 2.0.0-alpha2. In fact, with 2.0.0 the loop of 10,000 compilations only takes < 1 second vs the 1.x example above taking nearly 60 seconds to get through just 2,000 iterations. Why the huge difference?
```c#
var factory = new EngineFactory();
var engine = factory.ForEmbeddedResources(typeof(Program));
for(int i = 0; i < 10000; i++)
{
var model = new BaseMailModel
{
WebVaultUrl = "http://google.com",
SiteName = "Google"
};
var htmlContent = await engine.CompileRenderAsync("Templates.Welcome", model);
var textContent = await engine.CompileRenderAsync("Templates.Welcome.text", model);
}
```
Here's the memory profile for a 1,000,000 iteration test which only took 60 seconds to complete:


I have same problem @kspearrin with RazorLight 1.1.0, I'm run it in Docker Container and the container ever down in 5 minutes after get his container memory limitation.
@toddams Are you told me in the 2.0.0-alpha2 it's not happen? I will try it right now.
Please, let me know about the results @alexsandro-xpt .
@toddams everything is fine right now 2.0.0-alpha2 there are no more memory leak.
I'm rendering 14 template per second for email sending, apparently it's alright right now.
Thank you!
Awesome :) I'm glad it works for you
Until .NET framework support is added back, us old timers are stuck with this memory leak. Do you have any rough ETA on the 2.0-beta2 release? Or is there something I can do to work around this?
Most helpful comment