简体   繁体   中英

See memory adress of a variable when debugging in Visual Studio?

I have to use Visual Studio (Professional 2017) for developing rather than my favorite IDE. When debugging (and in particular stop-pointing), I am using the panel Locals (enabled via Debug > Windows > Locals ) which shows the values of variables at runtime. I would also love to be able to see the actual memory-address (eg to determine whether I am working with the same object, or to estimate how much memory is used by the objects) in that panel - a feature I have in other IDEs.

Is there any trick or plugin to see the memory addresses of variables during runtime in Visual Studio?

References

.NET is a garbage collected environment. When GC is performed objects are moved around in memory to consolidate free space. Thus looking at memory directly will, as GC collections happen, be looking at completely different objects. Unless you are working directly within the GC or analysing a memory dump (no execution, so no GC) directly looking at memory is unhelpful.

To look at a specific object – whatever references may reference it – make use of "Object IDs". In Locals, Autos, and Watch windows right click on a reference and select "Make Object Id". That generates a sequence symbol $1 , $2 , ... that 1. are used to annotate the display of any references to that object in the value column; 2. can be used directly across the debugger to look at that object (including in the immediate window within code fragments executed there: eg. ?local.Prop.Equal($2.Prop) that I used earlier this week). See https://docs.microsoft.com/en-us/visualstudio/debugger/watch-and-quickwatch-windows?view=vs-2019#bkmk_objectIds for a sample.

If you really want to look at memory for a variable you can use & in both the Watch and Intermediate window.

Eg if you have a variable foo , &foo displays the address, but as noted in another answer this could change due to compaction moving objects around during GC.

To open a Memory window

Make sure Enable address-level debugging is selected in Tools > Options (or Debug > Options) > Debugging > General.

Start debugging by selecting the green arrow, pressing F5, or selecting Debug > Start Debugging.

Under Debug > Windows > Memory, select Memory 1, Memory 2, Memory 3, or Memory 4. (Some editions of Visual Studio offer only one Memory window.)

or refer below links: https://docs.microsoft.com/en-us/visualstudio/debugger/memory-windows?view=vs-2017 Visual Studio, See variable's memory address in watch window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM