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?
-
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 TyngJan 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.– reneJan 31, 2012 at 22:48
-
Exactly, the size of the object and all the objects it references.– Zann AndersonJan 31, 2012 at 23:10
-
1Is 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 ShahFeb 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).– Zann AndersonFeb 29, 2012 at 13:57
3 Answers
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.
- Menu
- Debug
- Windows
- Show Diagnostic Tools
- Click
Take Snapshot
- Wait for the snapshot to be created.
- Click the blue hyperlinks in the
Objects
orHeap Size
columns - Look at inclusive size for your variable.
HTH.
EDIT: This feature is still present in VS 2022
-
1I will just add on very large solutions this may throw an out of memory exception in Visual Studio!– DibJun 19, 2019 at 7:30
-
1I'd like to add the link to this tool from MS: learn.microsoft.com/en-us/visualstudio/profiling/… Feb 20, 2021 at 16:27
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.
- Create a list of your object.
- Add only one object (for string properties add maximum of string length).
- Set a breakpoint before add object.
- Run the project
- Take a "Memory Usage Snapshot" (Tab: Memory Usage under Diagnostic Tools).
- Get another snapshot.
It is not exact, but it is a good guesstimate.