'Visual Studio watch window see object's size/memory footprint

Not sure if it's possible but I'm curious and I haven't had any luck finding this out so far. I'd like to know if anybody knows of a way to determine the size of an object in memory from within the VS watch window. Obviously a profiler could do this, but it would be super convenient just to grab a quick snapshot of this from within VS. Is this possible?



Solution 1:[1]

Not sure if this will help others landing on this question.

In VS2015 you can stop at a break point and use the Diagnostic Tools window.

  1. Menu
  2. Debug
  3. Windows
  4. Show Diagnostic Tools
  5. Click Take Snapshot
  6. Wait for the snapshot to be created.
  7. Click the blue hyperlinks in the Objects or Heap Size columns
  8. Look at inclusive size for your variable.

HTH.

EDIT: This feature is still present in VS 2022

Solution 2:[2]

  1. Create a list of your object.
  2. Add only one object (for string properties add maximum of string length).
  3. Set a breakpoint before add object.
  4. Run the project
  5. Take a "Memory Usage Snapshot" (Tab: Memory Usage under Diagnostic Tools).
  6. Get another snapshot.

It is not exact, but it is a good guesstimate.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mehdi