atifaziz / t5

T5 is T4 (Text Template Transformation Toolkit) for .NET Core
MIT License
79 stars 17 forks source link

Critical Memory Leak #7

Open endeffects opened 5 years ago

endeffects commented 5 years ago

When i try to generate code from compiled templates i'm running into a critical memory leak. The following code is for demonstration only.

var template = File.ReadAllText("Template.txt");

Parallel.For(0, 1000, (i) =>
{
    var compiledTemplate = engine.CompileTemplate(template);
    var result = compiledTemplate.Process();
});

Btw: All higher level template processing is effected too.

atifaziz commented 5 years ago

Can you share more details? Like:

My hunch is that the leak (if any) is coming from the use of Roslyn's C# scripting, which I have heard (from @filipw) is not designed to be used for long-running processes; that is, garbage compiled templates do not get collected. I could be wrong and so it should be confirmed with the Roslyn team/project.

endeffects commented 5 years ago

Thanks for the fast reply. Well the Template is a simple Class with some Placeholders, nothing special. There is also no Scripting involved which generates additional Code.

For testing i‘ve used the vs 2017 Monitoring Tools and the Taskmanager.

For now i just wrote this simple Stress Tool to check the Performance.

endeffects commented 5 years ago

Btw, i saw a similar behavior in the past by using the Microsoft implementation. I was able to fix this by using a singleton for the Engine and by disposing the AppDomain within the SessionHost.

I also tried this with your Library, but the CompiledTemplate Class throws an IndexOutBounceException by re-using it within the loop. Also i‘m not able to dispose the AppDomain by Design.