473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Jul 21 '05 #1
5 5718
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/p...b-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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #2
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("kernel3 2.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

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<uS**************@TK2MSFTNGP12.phx.gbl>...
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/p...b-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

Jul 21 '05 #3
Sunil,
Is this method wrong too...


Yes it's wrong, for a number of reasons.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #4
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

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<uT**************@tk2msftngp13.phx.gbl>...
Sunil,
Is this method wrong too...


Yes it's wrong, for a number of reasons.

Mattias

Jul 21 '05 #5
Sunil,
May I know the reasons...
OK...
<System.Runtime.InteropServices.DllImport("kernel 32.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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Geoff Pennington | last post by:
The memory consumed by aspnet_wp.exe just keeps going up (as tracked in Task Manager) until I restart it, so I must have a memory leak. If it makes a difference, my app makes considerable use of...
20
by: Philip Carnstam | last post by:
How come .Net applications use so much memory? Every application I compile uses at least 10 MB of memory, even the ones consisting of only a form and nothing else. If I minimize them though the...
1
by: Serdar C. | last post by:
hi again, i am writing a program that runs an application in every 1 minute, my problem is, even its a program that contains less than 100 lines of code, why does it takes 13mb in my memory? is...
14
by: John J. Hughes II | last post by:
Using the below code I am send multiple sterilized object across an IP port. This works fine if only one object is received at a time but with packing sometimes there is more then one object or...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
5
by: Sunil Menon | last post by:
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...
6
by: webinfinite | last post by:
Hi, How to find a STL class size, for example: string. I would like to know what the size of the implementation of this class, I am not interested in the size of its object but the class itself....
1
by: beena | last post by:
Hi All, Is there a way to calculate the maximum total memory ever used by the instance? Is db2mtrk the answer ... especially the high watermark option..... If I add up all the values......Will ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.