Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johannesg opened this issue Sep 18, 2017 · 8 comments · May be fixed by #493
Open

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

johannesg opened this issue Sep 18, 2017 · 8 comments · May be fixed by #493

Comments

@johannesg
Copy link

johannesg commented Sep 18, 2017

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
Copy link
Author

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?

@matthid
Copy link
Collaborator

matthid commented Sep 18, 2017

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)

@johannesg
Copy link
Author

It was already commented out when the entire class was introduced: a76daaa

I'll fix a PR with a test shortly

@johannesg
Copy link
Author

See #493

@IronRaver
Copy link

#463 is a duplicate of this.

@andreea-bizau
Copy link

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?

@cosminpopescu
Copy link

Any updates on this?

@leotsarev
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants