Skip to content

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

@johannesg

Description

@johannesg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions