RazorEngine icon indicating copy to clipboard operation
RazorEngine copied to clipboard

Cleanup helper domain not unloaded after dispose (Or: how to implement a recycle strategy)

Open johannesg opened this issue 9 years ago • 8 comments

Hello,

I'm trying to implement a recycle policy that after a certain number of recompilations will reload the AppDomain and cleanup memory and temp files. The only way I have found so far is to simply dispose IsolatedRazorEngineService and create a new one. This will unload AppDomain used for rendering and clean up some memory and it seemed fine at beginning.

Unfortunately, the appdomain created in CrossAppDomainCleanUp (called "CleanupHelperDomain_") is not unloaded during dispose and a new AppDomain will be created for every new IsolatedRazorEngineService, resulting in small memory leaks.

Example:

      using (var service = IsolatedRazorEngineService.Create())
      {
        service.AddTemplate("TestTemplate",
          "Hello @Model.Name " + new String(Enumerable.Repeat('A', 100000).ToArray()));
        service.Compile("TestTemplate", typeof(TemplateModel));
      }

// Now there is ONE CrossAppDomainCleanUp app domain

      using (var service = IsolatedRazorEngineService.Create())
      {
        service.AddTemplate("TestTemplate",
          "Hello @Model.Name " + new String(Enumerable.Repeat('A', 100000).ToArray()));
        service.Compile("TestTemplate", typeof(TemplateModel));
      }

// Now there are TWO CrossAppDomainCleanUp app domains

Is this a bug or am I doing something wrong? What is the best way to implement a recycle strategy?

Any help appreciated.

johannesg avatar Sep 18 '17 09:09 johannesg

If I uncomment this line, the problem goes away:

https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Compilation/CrossAppDomainCleanUp.cs#L368

Is there any reason that it is commented out?

johannesg avatar Sep 18 '17 09:09 johannesg

Interesting IIRC I couldn't make the line work. Can you send a pr with the line uncommented?

It could also be that I commented because of reported issues (but then git blame should show it, which it isn't)

matthid avatar Sep 18 '17 11:09 matthid

It was already commented out when the entire class was introduced: https://github.com/Antaris/RazorEngine/commit/a76daaad4e1d18f6a19c60dd9d248aa09733483d

I'll fix a PR with a test shortly

johannesg avatar Sep 18 '17 11:09 johannesg

See #493

johannesg avatar Sep 18 '17 12:09 johannesg

#463 is a duplicate of this.

IronRaver avatar Dec 07 '17 14:12 IronRaver

Hi there, is there any plan to get this issue fixed? I am experiencing the same problem. Are there any drawbacks to the proposed solution?

andreea-bizau avatar Mar 26 '18 22:03 andreea-bizau

Any updates on this?

cosminpopescu avatar Oct 14 '19 14:10 cosminpopescu

I'm not developer of RazorEngine, but I want to share our experience. We are migrated to Microsoft.NET.Sdk.Razor libraries. It's much more robust and maintained that "old" non-Core razor, and because it could be build compile-time it could be embedded into "old" ASP.NET MVC applications and ASP.NET Core application and basically everywhere. However, we don't need run-time compilation.

leotsarev avatar Oct 15 '19 06:10 leotsarev