473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3251
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******** ********@TK2MSF TNGP12.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@_spamkille r_bobpowell.net > дÈëÏûÏ¢
news:O4******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP12.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="+Marsh al.SizeOf(a).To String());
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******** *****@tk2msftng p13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > дÈëÏûÏ¢
news:O4******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP12.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@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
public struct aStruct

{

public int i;

public char c;

public double d;

}
aStruct a=new aStruct();

MessageBox.Show ("SizeOf aStruct="+Marsh al.SizeOf(a).To String());
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******** *****@tk2msftng p13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > дÈëÏûÏ¢
news:O4******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP12.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@_spamkille r_bobpowell.net > дÈëÏûÏ¢
news:#I******** ******@TK2MSFTN GP10.phx.gbl...
public struct aStruct

{

public int i;

public char c;

public double d;

}
aStruct a=new aStruct();

MessageBox.Show ("SizeOf aStruct="+Marsh al.SizeOf(a).To String());
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******** *****@tk2msftng p13.phx.gbl...
Marshal.SizeOf can only be used on types being marshaled.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > дÈëÏûÏ¢
news:O4******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP12.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.o rg> ????
news:#C******** ******@TK2MSFTN GP11.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
2090
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 variable... For example.... If I declare private System.Collections.ArrayList m_aArray;
1
8006
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 C:\Documents and Settings\jon.JONHS-LAP\My Documents\Visual Studio
5
2657
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 the size of: C1 class C2 class C1 instance Another question. If C1 has an instance method (1MB code), and i create 10
13
471
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
2496
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, Dennis
3
3513
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
5723
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 collected. I also noticed that when replaced some of the existing MFC dialogs with managed winforms that everything is still running in the same app domain.( No context change) So my question is this, what are the performance differences in using...
2
5789
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 function in the previously 6.0 code and I get the error noted below. What I don't understand is since I created this cpp as a new file in .net and inherited from a .net base class then why would I get this error? Both the class and base class to me...
5
2294
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.