21

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?

6
  • There is a profiler built in to visual studio, but I'm not aware of how to do this in the watch window without writing something custom.
    – Paul Tyng
    Jan 31, 2012 at 22:25
  • What is the size of an object in your definition? The same size you would get when write the object with the BinarySerializer to a stream? That would mean the object itself and all the objects it is referencing.
    – rene
    Jan 31, 2012 at 22:48
  • Exactly, the size of the object and all the objects it references. Jan 31, 2012 at 23:10
  • 1
    Is there a reason why you need to know the size? It is very tricky to find the "exact" size of an object in .NET - especially complex ones. I have been working on and off in WinDbg to understand the size of Application and Session cache data in an ASP.NET application - and am finding it very difficult to do. I have also worked on direct memory storage/access from .NET using unsafe code, pointers and memory mapped files - and have found that it is very tricky to store and work with complex objects. Feb 29, 2012 at 7:20
  • No major reason really, just wondering if it's possible as a way to get a handle on how large a given object is (ie a specific instance or how large instances of an object tend to be). Feb 29, 2012 at 13:57

3 Answers 3

20

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

2
1

I think this is not possible. See Size of a managed object (rather old but seems still valid).

You can have an object size in the Immediate window. See my answer in Find out the size of a .net object.

You can write a function in your code (adapted from above meant thread) and reference it from watch.

Public Function GetSerializedSize(myObject As Object) As Long
    Using ms As New IO.MemoryStream
        Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        bf.Serialize(ms, myObject)
        GetSerializedSize = ms.Position
    End Using
End Function

However, this is neither precise (I am getting 2 times smaller value for DatataTable) nor convenient (the value has to be manually refreshed), but it gives you some clue.

0
  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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.