Razorlight: Leaking Memory

Created on 19 Sep 2017  路  8Comments  路  Source: toddams/RazorLight

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:

image

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.

image

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!

Most helpful comment

7c31a1bd26ee70da803b5223ecf3d270ef8ba94711c8a9d74ef21e84571d317c

All 8 comments

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.

image

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:

image

7c31a1bd26ee70da803b5223ecf3d270ef8ba94711c8a9d74ef21e84571d317c

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

btran021111 picture btran021111  路  4Comments

nukec picture nukec  路  3Comments

Wolftousen picture Wolftousen  路  5Comments

Hubert-Rybak picture Hubert-Rybak  路  5Comments

danwalmsley picture danwalmsley  路  7Comments