Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

sizeof equiv' for managed objects?
pk r posted at Tuesday, March 02, 2004 7:18 AM

I'd like to be able to find the "size" of an managed object, bit like using sizeof (or len in VB). Anyone know how to do this? I suspect there maybe something in the type info perhaps something to do with reflection? BTW I'd like it to included any nested objects too.
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
Here is what Chris Brumme
Dexter Dotnetsky replied on Tuesday, March 02, 2004 9:52 AM

of Microsoft says:
"We don't expose the managed size of objects because we want to reserve the ability to change the way we lay these things out.  For example, on some systems we might align and pack differently.  For this to happen, you need to specify tdAutoLayout for the layout mask of your ValueType or Class.  If you specify tdExplicitLayout or tdSequentialLayout, the CLR’s freedom to optimize your layout is constrained.


If you are curious to know how big an object happens to be, there are a variety of ways to discover this.  You can look in the debugger.  For example, Strike or SOS (son-of-strike) shows you how objects are laid out.  Or you could allocate two objects and then use unverifiable operations to subtract the addresses.  99.9% of the time, the two objects will be adjacent.  You can also use a managed profiler to get a sense of how much memory is consumed by instances of a particular type.


But we don't want to provide an API, because then you could form a dependency over this implementation detail.


Some people have confused the System.Runtime.InteropServices.Marshal.SizeOf() service with this API.  However, Marshal.SizeOf reveals the size of an object after it has been marshaled.  In other words, it yields the size of the object when converted to an unmanaged representation.  These sizes will certainly differ if the CLR’s loader has re-ordered small fields so they can be packed together on a tdAutoLayout type."
Reply    Reply Using Power Editor
Dr. Dexter Dotnetsky is the alter ego of the eggheadcafe.com forums. An expert programmer and consummate gentleman in every way, he often pitches in to help out on unanswered or especially difficult forum posts, and to make his unique brand of snide comments or political "correctisms". Dr. Dotnetsky sez: "If we were all meant to get along, there would be no people who wait until all the groceries are rung up to start looking for their checkbook ."
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

Oh well
pk r replied on Thursday, March 04, 2004 7:04 AM

Thanks very much for clearing that up
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0