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

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
 
M

Mattias Sjögren

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
 
S

Sunil Menon

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
 
S

Sunil Menon

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
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top