473,378 Members | 1,360 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,378 software developers and data experts.

Object size in memory

I need to get the size of an objet in memory. I have tried:

System.IO.MemoryStream m = new System.IO.MemoryStream();

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter b = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
b.Serialize(m, Obj);
double size = Convert.ToDouble(m.Length);

but not everything is serializable. The thing is I want to be able to
take an arbitarty object, (since everything I am attempting to size is
comming out of the cache), and get the size of it. I am wondering if it
not possible to treat it like a stream or a byte array or something like
that so that it is easy to get the size. I don't need any attributes off
of the object, I just need the size. Anyone have any ideas?

-Cam

Nov 15 '05 #1
6 40832
cameron <ca****************@appdepot.com> wrote:
I need to get the size of an objet in memory. I have tried:

System.IO.MemoryStream m = new System.IO.MemoryStream();

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter b = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
b.Serialize(m, Obj);
double size = Convert.ToDouble(m.Length);

but not everything is serializable. The thing is I want to be able to
take an arbitarty object, (since everything I am attempting to size is
comming out of the cache), and get the size of it. I am wondering if it
not possible to treat it like a stream or a byte array or something like
that so that it is easy to get the size. I don't need any attributes off
of the object, I just need the size. Anyone have any ideas?


"Size" can be either misleading or meaningless when you consider
reference types. For instance:

public class Foo
{
string x;

public Foo (string x)
{
this.x=x;
}
}
Foo f1 = new Foo ("hello");
Foo f2 = new Foo ("hello");

Now, what is the size of f1 and f2? Combined, it should be double the
size of the Foo class instance itself (probably 12 bytes - an object
header of 8 bytes plus a reference) and the size of the string (16 or
20 bytes, at a guess) - but the size of each of them individually would
be the size of the Foo class instance itself plus the string - so the
whole is greater than the sum of the parts... but if you ignore the
string itself, it becomes meaningless in the other way.

So, what do you *really* mean, and what do you *really* want to try to
measure?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi cameron,
Serialization won't help. First because it will put more info than you maybe
expect. The stream is goint to be bigger then the object itself is. Second
because serializer (if you don't take any precautios) will serialize any
serializable objects your object references.

IMHO there is no way to get the size of a managed object. Even the size
might not be the same if you run the application on machines with different
configuration and/or different versions of .NET. If not stated otherways CLR
is free to rearange and layout the objects as it sees fit. That means that
you may end up with diffrent padding of the fields and different sizes in
memory.

The only size that you can get is the size on bytes of the unmanaget
equivalent of your type
Marshal.SizeOf(...)
--
B\rgds
100

"cameron" <ca****************@appdepot.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
I need to get the size of an objet in memory. I have tried:

System.IO.MemoryStream m = new System.IO.MemoryStream();

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter b = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
b.Serialize(m, Obj);
double size = Convert.ToDouble(m.Length);

but not everything is serializable. The thing is I want to be able to
take an arbitarty object, (since everything I am attempting to size is
comming out of the cache), and get the size of it. I am wondering if it
not possible to treat it like a stream or a byte array or something like
that so that it is easy to get the size. I don't need any attributes off
of the object, I just need the size. Anyone have any ideas?

-Cam

Nov 15 '05 #3
Fair enough. The Object is being placed in the Cache - I want to know
how much space is being taken up by that object, (in it's entirity), on
the Heap.

-Cam

Jon Skeet [C# MVP] wrote:
cameron <ca****************@appdepot.com> wrote:
I need to get the size of an objet in memory. I have tried:

System.IO.MemoryStream m = new System.IO.MemoryStream();

System.Runtime.Serialization.Formatters.Binary.B inaryFormatter b = new
System.Runtime.Serialization.Formatters.Binary.B inaryFormatter();
b.Serialize(m, Obj);
double size = Convert.ToDouble(m.Length);

but not everything is serializable. The thing is I want to be able to
take an arbitarty object, (since everything I am attempting to size is
comming out of the cache), and get the size of it. I am wondering if it
not possible to treat it like a stream or a byte array or something like
that so that it is easy to get the size. I don't need any attributes off
of the object, I just need the size. Anyone have any ideas?

"Size" can be either misleading or meaningless when you consider
reference types. For instance:

public class Foo
{
string x;

public Foo (string x)
{
this.x=x;
}
}
Foo f1 = new Foo ("hello");
Foo f2 = new Foo ("hello");

Now, what is the size of f1 and f2? Combined, it should be double the
size of the Foo class instance itself (probably 12 bytes - an object
header of 8 bytes plus a reference) and the size of the string (16 or
20 bytes, at a guess) - but the size of each of them individually would
be the size of the Foo class instance itself plus the string - so the
whole is greater than the sum of the parts... but if you ignore the
string itself, it becomes meaningless in the other way.

So, what do you *really* mean, and what do you *really* want to try to
measure?


Nov 15 '05 #4
cameron <ca****************@appdepot.com> wrote:
Fair enough. The Object is being placed in the Cache - I want to know
how much space is being taken up by that object, (in it's entirity), on
the Heap.


Right - as I said, that really depends on other objects etc. There's no
way of determining it, I'm afraid. If you know more about your objects
- like that the bulk of the size is going to be taken up by a byte
array which you have access to and which won't have duplicate
references within the cache, for instance, you could use the size of
that as a rough indication...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

Hi cameron,

Thank you for posting in the community!

I think Stoitcho and Jon have explained the reason why you can not get the
managed class instance size.

But I think what you want to do is getting the unmanaged class instance
size, so just as Stoitcho point out, you can use Marshal.SizeOf to returns
the unmanaged size of an object in bytes.

For more information, please refer to:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemruntimeinteropservicesmarshalclasssizeo ftopic.asp

==========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6

Hi cameron,

Does the community's reply make sense to you?

If you still have anything unclear, please feel free to post, we will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #7

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
9
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; ...
5
by: Phil Jones | last post by:
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...
8
by: al.cpwn | last post by:
Which sections of the 2003 standard should I study to understand the object model? I am looking to find information similar to that in Stanley B. Lippman's book, "Inside the C++ Object Model"
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 to create a new object to continue collecting the...
0
by: =?Utf-8?B?SkhhbGV5?= | last post by:
Our system is: IIS Server: dual Intel Xeon 2.80 GHz, 4 GB Ram Windows Server 2003 SP2 IIS 6.0 SQL Server: dual Intel Xeon 2.80 GHz, 4 GB Ram (separate server) Windows Server 2003 SP2 SQL...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.