469,934 Members | 2,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

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 4948
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.com>
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.com> wrote in message
news:MP************************@news.microsoft.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.com>
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.com>
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**************@tk2msftngp13.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******************@TK2MSFTNGP11.phx.gbl...
if you are using C# use sizeof method.

"Phil Jones" <ph************@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by pembed2003 | last post: by
6 posts views Thread by Jon Jagger | last post: by
1 post views Thread by Frank Rizzo | last post: by
reply views Thread by Diffident | last post: by
6 posts views Thread by Scirious | last post: by
4 posts views Thread by Dennis | last post: by
8 posts views Thread by filox | last post: by
6 posts views Thread by Christoph Boget | last post: by
reply views Thread by =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.