473,387 Members | 3,684 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,387 software developers and data experts.

How can I know the size of a managed reference type?

I'd like to calculate the memory size that my data structure cost, but I
could not find any API that I could calculate the size of an object. Could
anyone give me a work around?

Thanks!
Nov 16 '05 #1
7 3225
Marshal.SizeOf

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
I'd like to calculate the memory size that my data structure cost, but I
could not find any API that I could calculate the size of an object. Could
anyone give me a work around?

Thanks!

Nov 16 '05 #2
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> дÈëÏûÏ¢
news:O4**************@TK2MSFTNGP09.phx.gbl...
Marshal.SizeOf

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
I'd like to calculate the memory size that my data structure cost, but I
could not find any API that I could calculate the size of an object. Could anyone give me a work around?

Thanks!


Nov 16 '05 #3
public struct aStruct

{

public int i;

public char c;

public double d;

}
aStruct a=new aStruct();

MessageBox.Show("SizeOf aStruct="+Marshal.SizeOf(a).ToString());
This seems to work. What are you having trouble with?

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:up*************@tk2msftngp13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> дÈëÏûÏ¢
news:O4**************@TK2MSFTNGP09.phx.gbl...
Marshal.SizeOf

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
I'd like to calculate the memory size that my data structure cost, but I could not find any API that I could calculate the size of an object.

Could anyone give me a work around?

Thanks!



Nov 16 '05 #4
Bob,

Marshal.SizeOf return the size of the type when it is marshaled. In other
words the size of the unmanaged representation of the type. Type's size in
the managed environment doesn't make sense that's why there is no mathod or
instruction to find it.

The latter is not completely true.There is *sizeof* instruction in C#, which
however works only in usnafe mode, only with value types and AFAIK it
returns the managed size of the type. *sizeof* and Marshal.SizeOf may return
different values in some cases.

--

Stoitcho Goutsev (100) [C# MVP]
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
public struct aStruct

{

public int i;

public char c;

public double d;

}
aStruct a=new aStruct();

MessageBox.Show("SizeOf aStruct="+Marshal.SizeOf(a).ToString());
This seems to work. What are you having trouble with?

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:up*************@tk2msftngp13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> дÈëÏûÏ¢
news:O4**************@TK2MSFTNGP09.phx.gbl...
Marshal.SizeOf

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
> I'd like to calculate the memory size that my data structure cost,
but
I > could not find any API that I could calculate the size of an object.

Could
> anyone give me a work around?
>
> Thanks!
>
>



Nov 16 '05 #5
I'd like to calculate the memory size that my data structure cost, but I
could not find any API that I could calculate the size of an object. Could
anyone give me a work around?


Have a look at

http://blogs.msdn.com/cbrumme/archiv.../15/51326.aspx

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #6

Please see the title, I am asking : reference type.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> дÈëÏûÏ¢
news:#I**************@TK2MSFTNGP10.phx.gbl...
public struct aStruct

{

public int i;

public char c;

public double d;

}
aStruct a=new aStruct();

MessageBox.Show("SizeOf aStruct="+Marshal.SizeOf(a).ToString());
This seems to work. What are you having trouble with?

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:up*************@tk2msftngp13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> дÈëÏûÏ¢
news:O4**************@TK2MSFTNGP09.phx.gbl...
Marshal.SizeOf

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Lei Jiang" <co**@263.net> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
> I'd like to calculate the memory size that my data structure cost,
but
I > could not find any API that I could calculate the size of an object.

Could
> anyone give me a work around?
>
> Thanks!
>
>



Nov 16 '05 #7
Thanks

"Mattias Sjögren" <ma********************@mvps.org> ????
news:#C**************@TK2MSFTNGP11.phx.gbl...
I'd like to calculate the memory size that my data structure cost, but I
could not find any API that I could calculate the size of an object. Couldanyone give me a work around?


Have a look at

http://blogs.msdn.com/cbrumme/archiv.../15/51326.aspx

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #8

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

Similar topics

6
by: Mikael Janers | last post by:
I've tried to find answer to this question on the net but without success, so I hope someone here knows... How can I know if c# actualy creates an instance of an object when I declare a...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
5
by: Mariano Drago | last post by:
Hi there. How can i know how much memory is an instance taking, devided by internal instances. For examples lets say i have 2 classes, C1 and C2. C1 have an internal instance of C2 How can i know...
13
by: Daniel Jin | last post by:
I read somewhere that a bool is 1 byte unless if it's in a array, then it will be 2 bytes. is that true? if so, any explaination to why that is?
6
by: Dennis C. Drumm | last post by:
Is there a way to keep track of how much memory a stack object is consuming, considering it may contain a wide variety of objects that do not easily lend themselves to computing a size? Thanks,...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
4
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage...
2
by: andy6 via DotNetMonster.com | last post by:
I took a c++ 6.0 project and converted it to c++ .net 2005 project. I want to make a web service out of it. One of the new files I created was a cpp where I have the webmethod pointing to a...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.