472,353 Members | 1,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 5041
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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
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...
1
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...
0
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...
6
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...
4
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...
8
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...
4
by: diwakar09 | last post by:
Hi, class myclass: def __init__(self,i,j): ival = i sstrng = j
6
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...
0
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.