Click here to Skip to main content
15,889,808 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Visual Studio Online or Express? Pin
Rob Caron MSFT12-Aug-14 7:12
Rob Caron MSFT12-Aug-14 7:12 
GeneralRe: Visual Studio Online or Express? Pin
adamhill913-Aug-14 0:26
adamhill913-Aug-14 0:26 
AnswerRe: Visual Studio Online or Express? Pin
thatraja13-Aug-14 2:30
professionalthatraja13-Aug-14 2:30 
AnswerRe: Visual Studio Online or Express? Pin
sankarsan parida27-Aug-14 20:48
professionalsankarsan parida27-Aug-14 20:48 
QuestionRequesting help chasing a .net memory leak Pin
Joshua Gayou10-Aug-14 7:46
Joshua Gayou10-Aug-14 7:46 
AnswerRe: Requesting help chasing a .net memory leak Pin
Eddy Vluggen11-Aug-14 7:51
professionalEddy Vluggen11-Aug-14 7:51 
GeneralRe: Requesting help chasing a .net memory leak Pin
Joshua Gayou11-Aug-14 10:48
Joshua Gayou11-Aug-14 10:48 
AnswerRe: Requesting help chasing a .net memory leak Pin
Joshua Gayou11-Aug-14 13:08
Joshua Gayou11-Aug-14 13:08 
Right, found the answer. Thought I'd post how I did it here for anyone else who gets stuck on something like this.

First thing I did was a bunch of reading to school myself on memory leaks in .NET, how they're created, and how to hunt them down. I recommend the following articles:
Best Practices No. 5: Detecting .NET application memory leaks[^]

Memory Leak Detection in .NET[^]

I also downloaded Process Explorer and used that as a general tool to monitor application performance and judge if what I was doing had any useful impact at all.
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx[^]

It's important to note that before doing any of the above, I had already traced down the sections of code in my program that were creating the link. There are various ways to do this. For me, I had at least a general idea of the method calls causing the problem, so I began systematically commenting out blocks of code and re-running the app while watching Process Explorer. I eventually got to a point where the leak stopped occurring, at which point it became clear to me where this was happening. So lesson 1, it turns out that finding the leak can be pretty easy; fixing it might be not as straight forward.

I used sos.dll to dump my heap memory while running the program to see exactly what it was that was causing the usage to blow up. I'll note here that my platform is Win7 64 bit and my program was configured to target any CPU, which caused issues when I tried to click the "Enable unmanaged code debugging" check box in the project properties window. I had to set the target CPU to x86 to get that to work. Everything else as presented in the article I linked above concerning sos.dll worked as described (I used !dumpheap, !gcroot, etc commands from the immediate window in my case).

What I found was that I had a large number of System.WeakReference instances hanging around and not getting cleaned up. You can see where I discuss this earlier in this thread. From what I've read on the net, it seems that the biggest cause of this happening is when you instantiate object that add event handlers and then don't remove those handlers properly later when you're done with the object. I thought that this wasn't a problem in my code because I had rewritten it to minimize events and handlers as much as possible.

Even so, it turns out that I'm an idiot.

A long time ago, I wrote a generic interface for my class library that describes a basic set of debugging/logging events and handlers for any class that I wanted to be able to write information to my own home-rolled logging system. Very simple stuff; in a nutshell:

VB
Public Interface CoreEvents
    Event InfoMessage(ByVal e As EventInfo)
    Sub OnInfo(ByVal Message As String)
    Sub OnInfo(ByVal e As EventInfo)

    Event Err(ByVal e As EventErr)
    Sub OnErr(ByVal Description As String)
    Sub OnErr(ByVal e As EventErr)

    Event Exception(ByVal e As EventBaseException)
    Sub OnException(ByVal e As System.Exception)
    Sub OnException(ByVal e As EventBaseException)

    Event Debug(ByVal e As EventDebug)
    Sub OnDebug(ByVal Message As String)
    Sub OnDebug(ByVal e As EventDebug)

    Event Warning(ByVal e As EventWarning)
    Sub OnWarn(ByVal Message As String)
    Sub OnWarn(ByVal e As EventWarning)
End Interface


This works out fine when you have a few root classes that implement it that you expect to live throughout the life of the program. But when I wrote this, it worked out so well for me and made my life so easy that it became common practice for my to implement this in any new class I created.

So, I had some very low level items in the object hierarchy (node, signatures, etc, etc) all inheriting from a single master object: SortElement. SortElement is a custom object I wrote that anything can inherit from and become something that can be sorted rapidly in a Vector class that I wrote at some point. Guess what interface SortElement implements? Yep: CoreEvents. Duh.

So I went back and cleaned that up and remove that interface from anything that doesn't need it or shouldn't get it (specifically, objects likely to be instantiated and then dumped on a moment's notice like list nodes or helper classes like NodeSignature.

As an added measure, I also implemented the following pattern in my code for any objects that I wanted to make doubly sure would get sorted out by the GC:
http://msdn.microsoft.com/en-us/library/s9bwddyx%28v=vs.90%29.aspx[^]

I wrapped all that up and rebuilt and was met with an app that consumes no more than 35 MB of memory while running (before it was consuming something like 300 K per second perpetually until running into an OOM exception).

I also find it relevant to note that I did not have to rebuild the application under Release config (rather than Debug) as many forum posts out there suggest (I'm building against .net 3.5).

Anyway, hope some folks find this helpful. I'm actually rather thankful I came across this; it was maddening but well worth what I learned when dealing with it.
QuestionCS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
demoninside98-Aug-14 20:50
demoninside98-Aug-14 20:50 
AnswerRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
PIEBALDconsult8-Aug-14 20:56
mvePIEBALDconsult8-Aug-14 20:56 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
demoninside98-Aug-14 21:13
demoninside98-Aug-14 21:13 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
PIEBALDconsult9-Aug-14 5:24
mvePIEBALDconsult9-Aug-14 5:24 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
Dave Kreskowiak9-Aug-14 6:25
mveDave Kreskowiak9-Aug-14 6:25 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
demoninside98-Aug-14 21:22
demoninside98-Aug-14 21:22 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
PIEBALDconsult9-Aug-14 5:23
mvePIEBALDconsult9-Aug-14 5:23 
AnswerRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
Richard MacCutchan8-Aug-14 22:39
mveRichard MacCutchan8-Aug-14 22:39 
GeneralRe: CS0029: Cannot implicitly convert type 'System.DateTime' to 'var' Pin
demoninside99-Aug-14 0:35
demoninside99-Aug-14 0:35 
QuestionRetrieve information from UDL file Pin
Ashfaque Hussain4-Aug-14 19:23
Ashfaque Hussain4-Aug-14 19:23 
AnswerRe: Retrieve information from UDL file Pin
Eddy Vluggen5-Aug-14 2:37
professionalEddy Vluggen5-Aug-14 2:37 
QuestionControls resize to init after changing language Pin
ChriSSi0034-Aug-14 1:36
ChriSSi0034-Aug-14 1:36 
AnswerRe: Control resize to init after changing language Pin
Eddy Vluggen4-Aug-14 2:00
professionalEddy Vluggen4-Aug-14 2:00 
AnswerRe: Control resize to init after changing language Pin
Bernhard Hiller4-Aug-14 20:49
Bernhard Hiller4-Aug-14 20:49 
GeneralRe: Control resize to init after changing language Pin
ChriSSi0034-Aug-14 21:26
ChriSSi0034-Aug-14 21:26 
GeneralRe: Control resize to init after changing language Pin
Bernhard Hiller4-Aug-14 23:38
Bernhard Hiller4-Aug-14 23:38 
GeneralRe: Control resize to init after changing language Pin
ChriSSi0035-Aug-14 0:49
ChriSSi0035-Aug-14 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.