Updated on Kisan Patel
Here, I would like to demonstrate how to find memory leaks in .NET application.
To analyze memory leaks, first, you need to install dotnet-dump
command line tool:
dotnet tool install -g dotnet-dump
By using the below command, we can list the dotnet processes that dumps can be collected from
dotnet-dump ps
To collect a dump, use the below command:
dotnet-dump collect –process-id 1902
dotnet-dump collect -p 1902 //Short of –process-id
Next, analyze the core dump with the analyze command:
dotnet-dump analyze .\core_20190226_135850.dmp
The above command opens an interactive session where you can write commands
For Example:
We can use dumpheap
command to show manage heap information or memory leaks.
dumpheap -stat //only statistical summary
First, run the above command, It will list down MT
value with TotalSize
.
Next, run the below command to get the addresses of the objects.
dumpheap -mt 00007fff0297d698 //only method table objects
Here, 00007fff0297d698 is the MT value from the dumpheap -stat
command.
We can use gcroot
command to show information about references (roots) to an object at an address.
gcroot -all 000001d2ae50a6a8 // all means find all of the references to this address 000001d2ae50a6a8
Here is the result of the above command:
That’s It.
References:
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump
https://learn.microsoft.com/en-us/sysinternals/downloads/vmmap
https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/
https://www.microsoft.com/en-us/download/details.aspx?id=28567
https://learn.microsoft.com/en-us/shows/perfview-tutorial/