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.
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.
This links sums up pretty well the results I was getting: Memory Overflow: Having an increasing number of Microsoft.CSharp.RuntimeBinder.Semantics
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.
However we're using : Microsoft.Office.Interop.Excel
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 : Leak in RuntimeBinder when using "dynamic" keyword with __ComObject
For example, it's defined like this in the dll :
dynamic ActiveSheet { get; }
and we're using it like this (worksheet is an Excel.Worksheet object) :
worksheet = theWorkbook.ActiveSheet;
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.
Edit
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.
Here is a screen of what ANT profiler gives me :
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.
We're using Marshal.ReleaseComObject
everywhere we can, but I guess it's easy to miss one ... A check is required