2

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 :

enter image description here

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Lozah
  • 71
  • 1
  • 7
  • Can you post the method where the memory leak occurs? On the 2-3 hours before crashing, can you describe what the program is doing? At present, I don't think there is enough information here on a specific problem to get you a good answer. – Michael Feb 26 '19 at 14:18
  • 1
    Can you verify that the leaks are coming from the usage of ```dynamic``` or could it be that you're not releasing some Excel COM Objects properly? – devsmn Feb 26 '19 at 14:26
  • Ensure that when you have finished with ALL Excel COM objects that they are released with Marshal.ReleaseComObject(...) – PaulF Feb 26 '19 at 14:36
  • @Michael I tried to add more details in the question, hope it can help a little. As you can see i'm a bit lost myself – Lozah Feb 26 '19 at 15:06
  • Hmm. I've never seen `.ActiveSheet` used before. Personally, I've always used `Excel.Application.ActiveWorkbook.Sheets[1]` when writing to an Excel file. May be something worth looking into? – Jaskier Feb 26 '19 at 15:56
  • 1
    @Symon ActiveSheet was just an example. We use it one or two times in the program, but in the Excel.Range class we use also lot of properties as "Formula", "FormulaArray", "MergeCells", "Calculate" and much more which are also defined as dynamic in the parent class. Maybe your right, and I should look into an other way that don't use dynamics, or maybe check if there isn't an other Excel dll but if I can avoid it I would prefer, it would be very very lot of work to do, the project is huge – Lozah Feb 26 '19 at 16:05

0 Answers0