Note that there are some explanatory texts on larger screens.

plurals
  1. POpossible reasons for excessive memory consumption when instantiating a generic list of a managed VisualC++ type vs C# type?
    text
    copied!<p>I've spent the last couple of days attempting to pinpoint the source of a memory leak on a .NET application I'm responsible for maintaining.</p> <p>The application is mostly written in C#, but some parts reference a Visual C++ assembly. The memory leaks seem to center around the parts of the code that interact with the types defined in the Visual C++ assembly.</p> <p>It could very well be that the Visual C++ assembly is using unmanaged resources that are not being disposed properly, but I'm intrigued by the fact that merely initializing a generic list of Visual C++ types increases memory usage (perfmon.exe: Process|Private Bytes) significantly, even if no elements are added to the list.</p> <p>Please see the following sample code:</p> <pre><code>//defined in a referenced Visual CPP assembly public class ref VisualCPPType { private: String ^ a; String ^ b; public: VisualCppType(String ^ a, String ^ b) { this-&gt;a = a; this-&gt;b = b; } } //defined in the CSharp assembly public class CSharpType { private String a; private String b; public CSharpType(String a, String b) { this.a = a; this.b = b; } } //defined in the CSharp assembly public class ListWrapper { public List&lt;VisualCPPType&gt; _visualCppElementList; public List&lt;CSharpType&gt; _cSharpElementList; public ListWrapper(bool trueToInitVisualCppListOnly_falseToInitCSharpListOnly) { if (trueToInitVisualCppListOnly_falseToInitCSharpListOnly) { //Process|Working Set - Private -&gt; increases by ~5000K //Process|Private Bytes -&gt; increases by ~5000K //.NET CLR Memory|Bytes In All Heaps -&gt; remains constant _visualCppElementList = new List&lt;VisualCPPType&gt;(); } else { //Process|Working Set - Private -&gt; increases by ~750K //Process|Private Bytes -&gt; increases by ~500K //.NET CLR Memory|Bytes In All Heaps -&gt; remains constant _cSharpElementList = new List&lt;CSharpType&gt;(); } } } //defined in the CSharp assembly public class EntryPoint { public static void Main(String[] args) { //a utility class that can be used to obtain the same data displayed by perfmon.exe MemoryMonitor monitor = MemoryMonitor.NewMonitor(); ListWrapper list = new ListWrapper(Boolean.parse(args[0]); monitor.PrintMemoryDiff(); } } </code></pre> <p>As I noted in the comments, the mere act of instantiating a generic list of a Visual C++ element type appears to be a much more expensive operation than instantiating a generic list of a similar C# type.</p> <p>Can someone please explain this behavior? And if it seems like this could be one of the possible causes of the memory leak, offer some hints as to how I could go about fixing it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload