How can I see Objects Inside Heap & Stack in C#.Net
Asked Answered
U

3

10

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 deallocation in .Net. With your

If any document or link which will clear my doubts with your answer please share.

Ultravirus answered 25/9, 2011 at 17:45 Comment(2)
That's a lot of data and it likely won't tell you much. (Or worse, you'll come complaining that it never collects any garbage in the twenty lines you looked at.) If you have doubts on memory allocation and garbage collection, ask questions and/or seek high-level explanations, that's more likely to clear things up.Immerse
Sure, I will definately do this now onwardsUltravirus
I
5

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.

Infallible answered 25/9, 2011 at 17:51 Comment(0)
S
4

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.

Stamin answered 25/9, 2011 at 17:50 Comment(0)
H
1

You can also inspect the heap using the dotnet-dump tool:

dotnet tool install --global dotnet-dump
dotnet-dump collect [-h|--help] [-p|--process-id] [-n|--name] [--type] [-o|--output] [--diag]
Headquarters answered 24/8, 2022 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.