Page 1 of 1

Memory Leaks

Posted: Mon Mar 21, 2005 2:29 pm
by PK

I have just purchased my new copy of memory profiler and would like some information please.

I'm searching for memory leaks I have a form which holds a datagrid and every time I update the grid with new information I get the following data

Under System.Data DataColumn Live instances
Total 12 New 12 Removed 12 undisposed 144

Under System.DataTable
Total 1 New 1 Removed 1 undisposed 12

The undisposed value just keep climbing is this a memory leak

Posted: Mon Mar 21, 2005 2:55 pm
by Andreas Suurkuusk
The numbers presented do not indicate a memory leak. Since the number of removed instances equals the number of new instances, the memory usage of the program doesn't increase.

The Undisposed instances value only indicates the number of instances that have been garbage collected without being properly disposed. Even though it's preferable to always dispose all disposable instances, undisposed instances do not indicate a memory leak. Note that the number of undisposed instances is increased each time an instance becomes "undisposed" and it never decreases.

Failing to dispose an instance can in some cases create a memory leak, but since such an instance is not garbage collected, it will not be counted as undisposed.

UnDisposed Delta

Posted: Mon Oct 10, 2005 6:33 pm
by robertev
What if between two snapshots - one before opening a form and one right after closing that form - shows the following results (please check the UnDisposed):

Live Instances:
Total: 1
New: 0
Removed: 0
Delta: 0

Disposed Instances:
Total: 0
New: 0
Removed: 0
Delta: 0

UnDisposed Instances:
Total: 465
Delta: 91

If I open the same form three times and close it the delta of undisposed goes up 3x. Am I to assume that since there is a live instance and the delta of undisposed increases there is a leak - and the BinaryReader is not disposed properly?

Posted: Mon Oct 10, 2005 8:39 pm
by Andreas Suurkuusk
Since the number of New and Removed instances are 0, it doesn’t seem like you have a memory leak. Undisposed instances do not indicate that you have a memory leak. The fact that they’re undisposed indicates that they have actually been garbage collected, but without being properly disposed. Failing to dispose an instance usually result in less efficient memory utilization and lower performance, but doesn’t normally create a memory leak. A memory leak only occurs when you fail to dispose an instance whose Dispose method breaks a root path for an instance (e.g by removing itself from a global EventHandler).

In your case, the undisposed BinaryReader instances are not a problem. The BinaryReader class does not itself wrap any unmanaged resources, and it has no finalizer. The BinaryReader.Dispose does dispose its “BaseStream”, but as long as that Stream is disposed elsewhere there should not be any problem.