HOME
DOWNLOADS
CONTACT
Eneter Memory Leak Detector

Utility application to detect memory leaks caused by .NET events.
It can attach a running process and clearly report all memory objects which are referenced only from events.

Description

In the garbage collected environment the memory leak is a situation when an object is unused by the application logic but it cannot be released from the memory because somebody is still holding a reference to it. The most notorious way how this happens in .NET applications is via events. If a class instance subscribes to an event but then forgets to unsubscribe the garbage collector cannot release it because the event still holds its reference.

Eneter Memory Leak Detector is a utility console application which is focused to reveal memory leaks caused by events.

It investigates the allocated memory and clearly reports those objects which are referenced only from events. I.e. objects which seem to be not used by the application logic but forgot to unsubscribe so they cannot be released by the garbage collector.


Examples

Attachable Processes

Memory Leak Detector can investigate .NET based applications. It works for 32 bit or 64 bit .NET applications or .NET Core 3.0 based applications.

Get list of all attachable processes.
MemoryLeakDetector.exe -pids


Memory Leaks Detection

Following commands can be used to setup Memory Leak Detector to produce optimal memory leak reports.

Gets memory leak report for a specified process id.
MemoryLeakDetector.exe -leaks -p 2820 -f c:/tmp/memlog.txt

Gets memory leak report for a specified process id which is filtered for items containing the word TestingConsole.
MemoryLeakDetector.exe -leaks -p 2820 -n TestingConsole -f c:/tmp/memlog.txt

Gets memory leak report for a specified process id. The reported reference tree will have maximum level 4.
MemoryLeakDetector.exe -leaks -p 2820 -s 4 -f c:/tmp/memlog.txt

Gets memory leak report for a specified process id. The reported memory leak reference tree will have maximum level 2.
MemoryLeakDetector.exe -leaks -p 2820 -t 2 -f c:/tmp/memlog.txt

Gets memory leak report not only for events but for all delegates too. (It will report all objects which are referenced only from inside of events or delegates.
MemoryLeakDetector.exe -leaks -p 2820 -d c:/tmp/memlog.txt

Example of the report:

### 1 EventHandler Leak 1,048 Bytes 2 Objects 2c4ab0f3018 G0 L: .System.Byte[] 1024 Bytes <=TestingConsole.Leaker.myBytes 2c4ab0f3000 G0 L:*TestingConsole.Leaker 24 Bytes 2c4ab0f3418 G0 .System.EventHandler 64 Bytes =>_target 2c4ab0f9e28 G0 ..System.Object[] 40 Bytes => 2c4ab0f9e50 G0 ...System.EventHandler 64 Bytes =>_invocationList 2c4ab0f9e50 G0 @ ....System.EventHandler 64 Bytes =>_target 2c4ab0f2f60 G0 ....TestingConsole.Events.StandardEventProvider 24 Bytes =>SomethingOccured 2c4ab0f2e48 G0 .....TestingConsole.LeakProvider 80 Bytes =>myStandardEventProvider 0d8b792ed08 GCRoot:......TestingConsole.exe Main LocalVar TestingConsole.LeakProvider 0d8b792ed10 GCRoot:......TestingConsole.exe Main LocalVar TestingConsole.LeakProvider

The report above says the memory leak number 1 is of size 1048 bytes and consists of 2 objects. The report is visualized as a tree. At the top of the tree is the root of the memory leak (The attribute L indicates it is the leak and the * indicates it is the root of the memory leak.):

2c4ab0f3000 G0 L:*TestingConsole.Leaker 24 Bytes

Bellow the root of the memory leak are listed references which indicate the reference holding paths from the garbage collector memory root object(s) into that root of the memory leak. (Garbage collector root objects are objects which are used by the garbage collector as entry points to investigate references. Objects which can be reached from the memory root are considered as needed and the garbage collector will not release them from the memory.)

E.g. reference path from the garbage collector root to the memory leak:
(The character @ indicates the cyclic reference.)

2c4ab0f3000 G0 L:*TestingConsole.Leaker 24 Bytes 2c4ab0f3418 G0 .System.EventHandler 64 Bytes =>_target 2c4ab0f9e28 G0 ..System.Object[] 40 Bytes => 2c4ab0f9e50 G0 ...System.EventHandler 64 Bytes =>_invocationList 2c4ab0f9e50 G0 @ ....System.EventHandler 64 Bytes =>_target 2c4ab0f2f60 G0 ....TestingConsole.Events.StandardEventProvider 24 Bytes =>SomethingOccured 2c4ab0f2e48 G0 .....TestingConsole.LeakProvider 80 Bytes =>myStandardEventProvider 0d8b792ed08 GCRoot:......TestingConsole.exe Main LocalVar TestingConsole.LeakProvider

Above the root of the memory leak are listed references to other memory leak objects which are referenced from that root of the memory leak. E.g. the report says the root of the memory leak TestingConsole.Leaker has the member myBytes which is the reference to System.Byte[].

2c4ab0f3018 G0 L: .System.Byte[] 1024 Bytes <=TestingConsole.Leaker.myBytes 2c4ab0f3000 G0 L:*TestingConsole.Leaker 24 Bytes


Memory Leaks Statistics

Memory Leak Detector can produce statistics related to memory leaks. It contains usage of particular types and how much they contribute to the memory leak.

Gets memory leak statistics for a specified process id.
MemoryLeakDetector.exe -stats -p 2820 -f c:/tmp/memlog.txt

Gets memory leak statistics not only for events but for all delegates too. (It will collect statistics for all objects which are referenced only from inside of events or delegates.
MemoryLeakDetector.exe -stats -p 2820 -d -f c:/tmp/memlog.txt


Memory Dump

Following commands can be used to get reference trees of all objects. Each reference tree starts from the garbage collector memory root and then unwinds objects references.

Dumps all memory objects for a specified process id. (The report can get really big.)
MemoryLeakDetector.exe -dump -p 2820 -f c:/tmp/memlog.txt

Dumps all memory objects for a specified process id. The reported reference tree will have maximum level 3. MemoryLeakDetector.exe -dump -p 2820 -s 3 -f c:/tmp/memlog.txt

Dumps all memory objects for a specified process id. The report will be filtered for entries containing the word TestingConsole.
MemoryLeakDetector.exe -dump -p 2820 -n TestingConsole -f c:/tmp/memlog.txt


Copyright © 2010 Ondrej Uzovic. All rights reserved.