title: RazorEngine Memory Usage
body: <p>I have created a windows service to build and send emails. I am using the <a href="https://github.com/Antaris/RazorEngine" rel="noreferrer">Razor Engine</a> to parse the email templates. I am using a dynamic ExpandoObject to create the model. </p>
  
  <p>My problem is when each email is created and sent the memory is increasing but it is never been released. I have profiled the service with Ants Memory profiler(I haven't used this before) but it is showing the following results:</p>
  
  <p><strong>With Razor Engine</strong></p>
  
  <p>Parsing 200 emails with Razor.Parse(text,model)</p>
  
  <blockquote>
    <p>Generation 1: 12.9kb</p>
    
    <p>Generation 2: <strong>15.88mb</strong></p>
    
    <p>Large Object Heap: 290.9kb</p>
    
    <p>Unused memory allocated to .NET: 3.375mb</p>
    
    <p>Unmanaged: 69.51mb</p>
    
    <p>Total number of memory fragments: 197</p>
  </blockquote>
  
  <p><strong>No Razor Engine</strong></p>
  
  <p>Returning 200 emails unparsed text.</p>
  
  <blockquote>
    <p>Generation 1: 13.87kb</p>
    
    <p>Generation 2: 3.798mb</p>
    
    <p>Large Object Heap: 95.58kb</p>
    
    <p>Unused memory allocated to .NET: 4.583mb</p>
    
    <p>Unmanaged: 44.58mb</p>
    
    <p>Total number of memory fragments: 7</p>
  </blockquote>
  
  <p>With Razor the biggest generation 2 instances are:</p>
  
  <p>System.Reflection.Emit __FixUpData[] - 2,447,640 live bytes, 3,138 instances</p>
  
  <p>Has anyone any idea why the objects aren't being released and the Generation 2 is growing? Is there a way to have a new instance of the RazorEngine each time I want to parse a template and when its finished it will not be referenced and will go to the GC.</p>
  
  <p>Ive tried creating a new instance of Template service each time I parse a template but this hasnt made a difference</p>
  
  <pre><code>using (ITemplateService templateService = new TemplateService())
  {
       result = templateService.Parse&lt;ExpandoObject&gt;(text, model);
  }
  </code></pre>