title: Memory leak C# RuntimeBinder (dynamic keyword ?)
body: <p>I'm currently working on a huge C# project interacting with Excel. We have detected that with some excel files memory resources grow exponentially. After 2 or 3 hours Excel crashes.</p>
  
  <p>I installed ANT memory Profiler which seemed pretty good to analyze memory leaks in C# projects and I was pretty confused seeing that all the leaks come from classes I have never heard of. </p>
  
  <p>This links sums up pretty well the results I was getting: <a href="https://stackoverflow.com/questions/33080252/memory-overflow-having-an-increasing-number-of-microsoft-csharp-runtimebinder-s">Memory Overflow: Having an increasing number of Microsoft.CSharp.RuntimeBinder.Semantics</a></p>
  
  <p>The link says that the problem could come from the use of the dynamic keyword. BUT the problem is that we don't use this keyword in our code.</p>
  
  <p>However we're using : <code>Microsoft.Office.Interop.Excel</code> which is a dll we need to interact with Excel. After looking into their code, we saw this dynamic keyword is used in some of their classes to define their properties. We're latter using these same properties in our code in multiple places. But that means we have no power on these properties and that we can't use the solutions suggested in the last link or even in this one : <a href="https://stackoverflow.com/questions/33259334/leak-in-runtimebinder-when-using-dynamic-keyword-with-comobject/34115882">Leak in RuntimeBinder when using &quot;dynamic&quot; keyword with __ComObject</a></p>
  
  <p>For example, it's defined like this in the dll : </p>
  
  <pre><code>dynamic ActiveSheet { get; }
  </code></pre>
  
  <p>and we're using it like this (worksheet is an Excel.Worksheet object) : </p>
  
  <pre><code>worksheet = theWorkbook.ActiveSheet;
  </code></pre>
  
  <p>Furthermore I don't even know if the problem comes from these dynamic keywords, so maybe I'm looking completely away from the real solution.</p>
  
  <h2>Edit</h2>
  
  <p>I'm not able to know in what method the crash occurs, we're using threads for calling the API, the informations we recover are put inside excel cells. The API is called multiple times per second. So I guess some informations are kept in memory when the cell is updated instead of being deleted.</p>
  
  <p>Here is a screen of what ANT profiler gives me : </p>
  
  <p><a href="https://i.stack.imgur.com/TQAsU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TQAsU.png" alt="enter image description here"></a></p>
  
  <p>Since we never use these CSharp classes I don't know what is at the source of these leaks nor in what method it happens, and since it's threaded and automated there is no user action that could tell us when exactly the leak occurs. </p>
  
  <p>We're using <code>Marshal.ReleaseComObject</code> everywhere we can, but I guess it's easy to miss one ... A check is required</p>