473,419 Members | 2,499 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,419 software developers and data experts.

SizeOf for managed types

Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK
Nov 17 '05 #1
9 6291

"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK


There is no reliable way to know the size of managed objects from within
your application code.
Why would you ever need to check the size of managed types?

Willy.
Nov 17 '05 #2
KH
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK

Nov 17 '05 #3
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

....
Test t = new Test();
t.s = "guess the object size";
Console.WriteLine(System.Runtime.InteropServices.M arshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.


"KH" <KH@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK

Nov 17 '05 #4
Hi,

Thanks for your answer, the problem that I am trying to solve is a memory
consumption issue. I have a simple structure with three fields, they are
populated from the database, if I dump them to the disk it consumes 300Mb but
if I load it in memory in a hashtable the memory consumption is 1.5Gb...
bizarre, so I wanted to check what is going on...

Anyway, Thanks for the answer.
Cheers

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Willy Denoyette [MVP]" wrote:

"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK


There is no reliable way to know the size of managed objects from within
your application code.
Why would you ever need to check the size of managed types?

Willy.

Nov 17 '05 #5
Three fields of what type?
Beware that .NET strings are unicode, if you load ascii strings from a DB
the size in memory will double.
Willy.
"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
Hi,

Thanks for your answer, the problem that I am trying to solve is a memory
consumption issue. I have a simple structure with three fields, they are
populated from the database, if I dump them to the disk it consumes 300Mb
but
if I load it in memory in a hashtable the memory consumption is 1.5Gb...
bizarre, so I wanted to check what is going on...

Anyway, Thanks for the answer.
Cheers

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Willy Denoyette [MVP]" wrote:

"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
> Hi,
>
> Does anybody know how to get the size in bytes of a managed type?
>
> I have a hashtable and I want to check the size in bytes. I can get the
> size
> using sizeof for value types with prmitives types (like int32, etc).
>
> Thanks
>
>
> --
> Salvador Alvarez Patuel
> Exony Ltd - London, UK


There is no reliable way to know the size of managed objects from within
your application code.
Why would you ever need to check the size of managed types?

Willy.

Nov 17 '05 #6
So Willy,

What do you recommend?

I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf (t)" give me?

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

...
Test t = new Test();
t.s = "guess the object size";
Console.WriteLine(System.Runtime.InteropServices.M arshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.


"KH" <KH@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:

Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK


Nov 17 '05 #7
Inline

Willy.

"SevDer" <do****@sevder.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So Willy,

What do you recommend?
Nothing, as I said before there is no reliable way to know the exact size of
a managed object from within your managed code.
I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Use a CLR profiler to measure the size of your objects, of course if the
tend to be variable in size you should probably need to calculate an
average.
Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf (t)" give me?

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

...
Test t = new Test();
t.s = "guess the object size";

Console.WriteLine(System.Runtime.InteropServices.M arshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.


"KH" <KH@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
--
Salvador Alvarez Patuel
Exony Ltd - London, UK



Nov 17 '05 #8
Thanks for the advice.
--

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
Inline

Willy.

"SevDer" <do****@sevder.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So Willy,

What do you recommend?


Nothing, as I said before there is no reliable way to know the exact size of
a managed object from within your managed code.

I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Use a CLR profiler to measure the size of your objects, of course if the
tend to be variable in size you should probably need to calculate an
average.

Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf (t)" give me?

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

...
Test t = new Test();
t.s = "guess the object size";

Console.WriteLine(System.Runtime.InteropService s.Marshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.


"KH" <KH@discussions.microsoft.com> wrote in message
news:74**********************************@micro soft.com...
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:

>Hi,
>
>Does anybody know how to get the size in bytes of a managed type?
>
>I have a hashtable and I want to check the size in bytes. I can get the
>size
>using sizeof for value types with prmitives types (like int32, etc).
>
>Thanks
>
>
>--
>Salvador Alvarez Patuel
>Exony Ltd - London, UK

Nov 17 '05 #9
Wow. Profiler works very cool but too slow of course.

--

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
Inline

Willy.

"SevDer" <do****@sevder.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So Willy,

What do you recommend?


Nothing, as I said before there is no reliable way to know the exact size of
a managed object from within your managed code.

I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Use a CLR profiler to measure the size of your objects, of course if the
tend to be variable in size you should probably need to calculate an
average.

Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf (t)" give me?

SevDer
http://www.sevder.com

Willy Denoyette [MVP] wrote:
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

...
Test t = new Test();
t.s = "guess the object size";

Console.WriteLine(System.Runtime.InteropService s.Marshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.


"KH" <KH@discussions.microsoft.com> wrote in message
news:74**********************************@micro soft.com...
See System.Runtime.InteropServices.Marshal.SizeOf()
"Salvador" wrote:

>Hi,
>
>Does anybody know how to get the size in bytes of a managed type?
>
>I have a hashtable and I want to check the size in bytes. I can get the
>size
>using sizeof for value types with prmitives types (like int32, etc).
>
>Thanks
>
>
>--
>Salvador Alvarez Patuel
>Exony Ltd - London, UK

Nov 17 '05 #10

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

Similar topics

2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
4
by: Paul | last post by:
I'm porting a C++ function which reads a file in binary mode, sometimes reading 2 bytes into an unsigned short, 4 bytes into a long, etc into a C# implementation. When using the FileStream.Read...
3
by: Dave | last post by:
I'm at a point where I would really like to focus in on learning .NET but am having a hard time deciding which language to use. I learned to program in C++ but have spent quite a bit of time using...
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...
15
by: stand__sure | last post by:
Having recently had a need to use ZeroMemory from the kernel32.dll, I "discovered" that there is apparently no .NET equivalent to the c/c++ sizeof operator -- one does exist in the Marshall...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
4
by: raylopez99 | last post by:
Marshal..::.SizeOf Method (Object) and sizeof() do not exist for objects in a managed language, right? I was trying to find how much memory is taken up by an array of ints, and I could not use...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.