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?

Zann Anderson
  • 4,767
  • 9
  • 35
  • 56
  • 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 '12 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 '12 at 22:48
  • Exactly, the size of the object and all the objects it references. – Zann Anderson Jan 31 '12 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. – Dhwanil Shah Feb 29 '12 at 07: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). – Zann Anderson Feb 29 '12 at 13:57
  • No. This only makes sense for tree-like structures (where objects only point to objects further down their tree-like structure, and never up or out). The reason is that as soon as a single object in the tree-like structure points outside of the tree, suddenly your "memory usage" will shoot upwards, because suddenly you're counting the memory of some random subset of all live objects. Now when you add to that the fact that there are native resources and classes outside of your control in almost any GC-closure, you suddenly find that any such profiling is probably meaningless. – SecurityMatt Mar 11 '12 at 23:42

3 Answers3

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

Dib
  • 2,001
  • 2
  • 29
  • 45
  • 1
    I will just add on very large solutions this may throw an *out of memory exception* in Visual Studio! – Dib Jun 19 '19 at 07:30
  • 1
    I'd like to add the link to this tool from MS: https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage-without-debugging2?view=vs-2019 – Gustavo E. Hennemann Feb 20 '21 at 16:27
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.

Community
  • 1
  • 1
IvanH
  • 5,039
  • 14
  • 60
  • 81
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.

Mehdi
  • 903
  • 3
  • 10
  • 26