How can I calculate size of the memory consumed by a collection of objects?

  • Thread starter Thread starter Sunil Menon
  • Start date Start date
S

Sunil Menon

Dear All,
Given a Hash Table containing "n" objects (each having many
properties) - is it possible to know how much memory that hash table
has taken - in a simple console application?

Please help.
The following is the solution I have at present:
To serialize the hashtable in to memorystream.
Dim m As New MemoryStream()
Dim b As New BinaryFormatter()
b.Serialize(m, Me)
m.Length // will give the length in bytes

This can be implemented in a console application.

Is this the right method? Is there some other simple method?

TALIA
Many Regards
Sunil
 
Sunil,
Given a Hash Table containing "n" objects (each having many
properties) - is it possible to know how much memory that hash table
has taken - in a simple console application?

No, not from within your code.

http://blogs.gotdotnet.com/cbrumme/permalink.aspx/6191a0fa-0c5d-4eb9-b3bb-d567996e6fc9

Is this the right method? Is there some other simple method?

That gives you the size of the objects when serialized, which isn't
necessarily the same as the in-memory size.



Mattias
 
Dear Mattias,
Thanks for replying to my mail...I guess I am trying to dig a well
in a desert to find water....just one more query...I tried the
following...
<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Public Shared Function GlobalSize(ByVal hMem As Long) As Long
End Function
Dim FrameString As String = "Hello World"
Dim oHandle As GCHandle = GCHandle.Alloc(FrameString)
Dim oPtr As IntPtr = New IntPtr(4)
oPtr = GCHandle.op_Explicit(oHandle)
Dim oLong As Long = CType(oPtr.ToInt32, Long)
Dim oSize As Long = GlobalSize(oLong)


But every time I got the same value in oSize.
5636130963718144
5636130963718144
Is this method wrong too...

Please help...

TALIA
Many Regards
Sunil
 
Dear Mattias,
Thanks for your reply and having the patience to reply to my
"unrealistic" queries...:-)
May I know the reasons...

P.S: You could go the bathroom and laugh your heart out...:-) but
please reply...

Thanks & Regards
Sunil
 
Sunil,
May I know the reasons...
OK...

<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Public Shared Function GlobalSize(ByVal hMem As Long) As Long
End Function

The function signature is incorrect. Long is 64 bits, whereas pointers
on Win32 are 32 bits. That explains why you get such large numbers
returned. A correct signature would be

Public Shared Function GlobalSize(ByVal hMem As IntPtr) As Integer

Dim FrameString As String = "Hello World"
Dim oHandle As GCHandle = GCHandle.Alloc(FrameString)
Dim oPtr As IntPtr = New IntPtr(4)
oPtr = GCHandle.op_Explicit(oHandle)

The value you get when you cast a GCHandle to an IntPtr is *not* a
pointer to the object. If the object has been pinned (GCHAndle
allocated with GCHandleType.Pinned), you can get a pointer from
GCHandle.AddrOfPinnedObject.

Dim oSize As Long = GlobalSize(oLong)

Since managed objects are on the GC heap, you can be pretty sure that
the object memory hasn't been allocated with the Global* family of
memory functions. Therefore, you can't expect GlobalSize to return the
value you expect.



Mattias
 
Back
Top