473,695 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the size (ie. in bytes) of an in-memory object

Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring
the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.

Is there???

Thanks everyone....

Phil (Aotearoa :: NZ)
Nov 22 '05 #1
5 5123
Phil Jones <ph************ @hotmail.com> wrote:
Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring
the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.


You really need to define exactly what you mean by the in-memory size
of an object. Most objects refer to other objects, for instance. If you
had two objects of class Foo, declared below, but they both referred to
the same large string, what would the size of each object be? Just the
size needed to store the string reference but not the string itself, or
the size needed to store everything?

class Foo
{
string bar;
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 22 '05 #2
I'm wanting to manage the size of a hash-table, and do certain things when
the contents (that is the cumulative size of the object's contained within
it) reaches a given threshold.

So defining the question more accurately within your guidelines, I'm looking
for the size to store everything.

Thanks....
---
Phil
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Phil Jones <ph************ @hotmail.com> wrote:
Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.


You really need to define exactly what you mean by the in-memory size
of an object. Most objects refer to other objects, for instance. If you
had two objects of class Foo, declared below, but they both referred to
the same large string, what would the size of each object be? Just the
size needed to store the string reference but not the string itself, or
the size needed to store everything?

class Foo
{
string bar;
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 22 '05 #3
Phil Jones <ph************ @hotmail.com> wrote:
I'm wanting to manage the size of a hash-table, and do certain things when
the contents (that is the cumulative size of the object's contained within
it) reaches a given threshold.

So defining the question more accurately within your guidelines, I'm looking
for the size to store everything.


Then you need to more carefully define "everything " otherwise you'll
end up with "size of objects outside hashtable + size of objects inside
hashtable > total size" for instance (if they share objects, etc).

I suspect you'll find you basically can't do this though. Even
serialising would only give you some kind of guesstimate.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 22 '05 #4
if you are using C# use sizeof method.

"Phil Jones" <ph************ @hotmail.com> wrote in message
news:O1******** ******@tk2msftn gp13.phx.gbl...
Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring
the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.

Is there???

Thanks everyone....

Phil (Aotearoa :: NZ)

Nov 22 '05 #5
Cool...thanks for that.

As I'll be wanting at look for the size of reference types too this won't
really work - so I guess it's like Jon suggested above - this kind of thing
really isn't possible.

Cheers everyone.
---
Phil
<tm****@hotmail .com> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
if you are using C# use sizeof method.

"Phil Jones" <ph************ @hotmail.com> wrote in message
news:O1******** ******@tk2msftn gp13.phx.gbl...
Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.

Is there???

Thanks everyone....

Phil (Aotearoa :: NZ)


Nov 22 '05 #6

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

Similar topics

9
7213
by: pembed2003 | last post by:
Hi all, If I have the following: class Foo{ void something(void){;} }; and then later I say: Foo f;
6
2467
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following. It's not guaranteed behaviour but it seems to work. Doesn't lose verifiability either. Enjoy. Cheers Jon Jagger namespace JSL
1
5975
by: Frank Rizzo | last post by:
I am doing some memory profiling work and I am trying to figure out the minimum size of an object. The object has a bunch of variables that are other objects (hashtables, array lists, and whatnot). How can I measure the size of such an object? C# provides a sizeof operator, but only for value types. Regards, Robert
0
848
by: Diffident | last post by:
Hello All, I just read an article which says that memory allocation is done in segments where each segment is a contiguous block of 64 MB in size. Generally Large objects which are more than 85K in size are allocated in the LOH large object heap. How can a developer calculate the size of the object? Is there any formula for this :)
6
14335
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the data and send the other one to a thread that records it's content to the disk. How do I do such thing? TIA, Scirious.
4
2768
by: Dennis | last post by:
Is there any way to get the memory size for an instance of an object, like in bytes or something like that. I have an UNDO feature on one of my applications and I'd like to be able to display a warning when the UNDO que got to a certain size. I can't use UndoQue.Count since the objects in the que can vary from strings to DataTable Rows to other large objects. -- Dennis in Houston
8
2730
by: filox | last post by:
is there a way to find out the size of an object in Python? e.g., how could i get the size of a list or a tuple? -- You're never too young to have a Vietnam flashback
4
3859
by: diwakar09 | last post by:
Hi, class myclass: def __init__(self,i,j): ival = i sstrng = j
6
14111
by: Christoph Boget | last post by:
If I had an array like so: var bob = ; I can find out the size of the array by doing bob.length; Is there a comparable way to get the size of an object? So if I did:
0
1700
by: =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post by:
I need to access classic ASP intrinsic objects and their properties from a ..net assembly wrapped to COM. The COM .net assembly is then instanciated from a classic ASP page with Server.CreateObject(). I'm trying to use the Microsoft Transaction Server this way: Type typeMtx = Type.GetTypeFromProgID("MTxAS.AppServer.1"); object mtxobject = Activator.CreateInstance(typeMtx); // Getting classic ASP object context
0
8650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8586
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8838
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2290
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1987
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.