Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 962
Zann Anderson Avatar asked Jan 31 '12 22:01

Zann Anderson


2 Answers

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.

like image 35
IvanH Avatar answered Nov 15 '22 23:11

IvanH


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

like image 58
Dib Avatar answered Nov 15 '22 23:11

Dib