| 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 |
| February |
0 |
$0.00 |
0 |
| January |
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 ."
Dexter Dotnetsky Create Your Badge |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
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 |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
|
|
|
Didn't Find The Answer You Were Looking For? |
| EggHeadCafe has experts online right now that may know the answer to your question. We pay them a bonus for answering as many questions as they can. So, why not help them and yourself by becoming a member (free) and ask them your question right now? |
| Ask Question In Live Forum |
|
| If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application. |
| Ask Question In Chat Chaos |
|