Memory Leak Identification

May 27, 2015 at 6:10 PM
Hi Govert,

I have created a DNA addin but am wondering whether there is some sort of memory leak or if it is normal Excel behaviour. I'm not experiencing any problems with excel remaining in Task Manager.

My main concern is that I have a static C# dictionary to which other objects ( C# not Excel) are added whenever a re-calculation is requested. The dictionary is set to a new dictionary at each re-calculaton. I would think that the GC would normally take care of this. What I notice is that after starting the CLR memory is 28MB and the Private Bytes around 129MB, but after each recalculation the private bytes quickly rises but clr heap memory rises very slowly.
It takes a number of calculations before the private bytes reaches a plateau but then a different sheet different calculations and memory usage rises. Then to further confound matters there can be a small drop before it stabilizes again.

I guess my questions are :-
  1. Does Excel ever free up memory as it seems to continuously rise stabilize and rise again on different actions.
  2. I understand that DNA provides a native proxy for the C# code. Am I correct in understanding that as far as C# code that is free of any excel/com objects there is nothing that can cause a memory leak? So as long as repeating the same activity with the same data leads to a stable private bytes there is no leak?
Can I stop worrying?
Coordinator
May 27, 2015 at 9:16 PM
Edited May 27, 2015 at 9:18 PM
It will help to figure out whether the memory being used is part of the .NET managed heap or not.

For this you can make a macro that forces garbage collection and show the total managed memory usage:
public static void CheckMemory()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

    long managedMemory = GC.GetTotalMemory(true);

    // Somehow log or display managedMemory
}
If this number is fairly stable, you know that the increase in the process's working set is not due to your .NET code.

For managed memory, you should be able to attach a profiler to figure where the memory is going.
There was a recent issue with Excel-DNA regarding the RTD servers, fixed more recently that version 0.32: https://exceldna.codeplex.com/discussions/581717

If the memory that concerns you is not managed memory, it's one of two things:
  • Excel-DNA marshaling leaking memory
  • Excel itself using more memory over time.
There are a number of cases where Excel seems to use increasing amounts of memory. For example, there is the Undo stack. And Excel seems to have some internal string caching, so writing different strings to cells will cause increasing memory usage.
The general strategy for these is to make a minimal reproduction using either VBA or a native C add-in, and then report to Microsoft.

If you struggle the pin the issue down, I'd be happy to help if you can make a minimal project that reproduces the issue.

-Govert
May 28, 2015 at 1:11 PM
Hello Govert,

Thanks for that advice. I clearly do have a problem as I suspected, Perfmon just seemed to muddy the waters.

I think I have a pretty good idea of where the problem is, I think I might be able to create a cut-down .dna file to see if I can reproduce the problem.

Regards

pianoboy