Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Size of an object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ThunderMusic - 09 Feb 2007 19:28 GMT
Hi, is there an easy way to know how much memory an instance of an object
takes?

I mean, there is the sizeof(type), but this is not what I want...  the
sizeof(type) returns the size of all the members of the class, but if the
class contains reference types like a collection or a string, it can't
tell...

What I need to know is, if there is a collection of strings, with each
strings taking 100kb and having 5 strings in the collection, I want to have
500kb (or approx) as a return...  is there an easy, thread-safe way?

Thanks

ThunderMusic

btw, I'm using c# with framework 2.0
Ignacio Machin ( .NET/ C# MVP ) - 09 Feb 2007 19:42 GMT
Hi,

| Hi, is there an easy way to know how much memory an instance of an object
| takes?
[quoted text clipped - 3 lines]
| class contains reference types like a collection or a string, it can't
| tell...

If you look into the archives you will see this is a recurrng question with
no clear answer.

No, you have no way to know how "big" an object is if you take into account
the references it can hold (or the references those referenced instnaces can
hold)

Signature

Ignacio Machin
machin AT laceupsolutions com

Göran Andersson - 10 Feb 2007 18:24 GMT
> Hi, is there an easy way to know how much memory an instance of an object
> takes?
[quoted text clipped - 13 lines]
>
> btw, I'm using c# with framework 2.0

You can add the size of the members in the object to get an approximate
value of how much memory it uses.

There is no way to get a definite size, though. Take a simple examle:

string a = new String('*', 5000);
string b = a;

If you look at each variable, it uses around 10000 bytes, but if you
look at them together, they don't use 20000 bytes in total. So, which of
them is it that is using the memory?

Signature

Göran Andersson
_____
http://www.guffa.com


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.