How can I see Objects Inside Heap & Stack in C#.Net
Is it possible to see contents of stack and heap after every line of execution. I want to see it as it will give clear idea about memory allocation and开发者_StackOverflow deallocation in .Net. With your
If any document or link which will clear my doubts with your answer please share.
SOS or PssCor are a good place to start, along side WinDbg.
Once you've got that sorted out; attach WinDbg to your process, the load the debugger extension. For example:
.load C:\pathtoextensions\psscor4.dll
After that, you can issue the !dumpheap
or !dumpstack
commands.
The output of both of these commands is very raw. !dumpheap -stat
will give you a "statistical" overview of your heap. The type, the number allocated, and the bytes in total for all allocations.
This isn't a super straightforward task. It'll take a while to get enough practice with WinDbg if you haven't used it before.
What you can do is set a breakpoint on a method using !bpmd
, and use the commands mentioned above, then step over using the p
command, and re-run the commands.
I'm sure there are other commercial tools like ANTS Profiler or dotTrace that may get the job done - but I don't have a lot of experience with either tool.
Once you've gotten started, you can ask (new) more specific questions about SOS or Psscor.
Stack:
var stackInfo = new StackTrace();
Heap? Nope, you'd need to use a profiler, debugger, or appropriate APIs. Not a straightforward task. If you try it and have difficulties, best to ask a more specific question.
You can also inspect the heap using the dotnet-dump
tool:
- https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump
dotnet tool install --global dotnet-dump
dotnet-dump collect [-h|--help] [-p|--process-id] [-n|--name] [--type] [-o|--output] [--diag]
精彩评论