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

There is no sizeof in a managed language, right?

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 sizeof or Marshal.SizeOf(), since apparently these
functions are for unmanaged code.

With references and managed languages, what do you use to figure this
out?

RL
Sep 30 '08 #1
4 1189
raylopez99 <ra********@yahoo.comwrote:
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 sizeof or Marshal.SizeOf(), since apparently these
functions are for unmanaged code.

With references and managed languages, what do you use to figure this
out?
The thing is, there's no easy answer because of the way objects
reference each other. There are two things to consider:

1) The object's direct footprint on the heap (or the footprint of a
value type, wherever it's stored).

2) The memory which is taken up by that object *and other objects it
references* (etc, recursively). This will give you some idea of how
much memory could be reclaimed if the object became eligible for
garbage collection - but only if *other* (ineligible) objects didn't
reference some of the same objects.

For an array of ints, however, it's basically the size*4 + some
overhead (about 12 bytes at a guess, but it could be a bit more - not a
lot).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 30 '08 #2


Jon Skeet [ C# MVP ] wrote:
For an array of ints, however, it's basically the size*4 + some
overhead (about 12 bytes at a guess, but it could be a bit more - not a
lot).

--
I took this to mean: sizeof(int) * 4 * N, where N= array elements?
And sizeof(int) is two bytes on most machines?

So, with the overhead, 8 to 12 bytes per element * a 1E6 element array
(million element array) is 12 M bytes?

BTW, what's the biggest array you've had in C# in memory? I have two
gigs RAM and I don't think a 1E6 int array is too big, do you?

RL
Oct 1 '08 #3
On Sep 30, 7:06*pm, raylopez99 <raylope...@yahoo.comwrote:
Jon Skeet [ C# MVP ] wrote:
For an array of ints, however, it's basically the size*4 + some
overhead (about 12 bytes at a guess, but it could be a bit more - not a
lot).
--

I took this to mean: *sizeof(int) * 4 * N, where N= array elements?
And sizeof(int) is two bytes on most machines?
int is basically just a keyword alias for Int32. So sizeof(int) would
be 4 bytes on all machines.
So, with the overhead, 8 to 12 bytes per element * a 1E6 element array
(million element array) is 12 M bytes?
The overhead is for the entire array. Each element of a int array
would still be 4 bytes.

One oddity though, if the int's were boxed first then I believe there
would be an extra 8 bytes of overhead per element since reference
types need 4 bytes for the sync block and 4 bytes for the method
table. Maybe someone else can confirm that.
BTW, what's the biggest array you've had in C# in memory? *I have two
gigs RAM and I don't think a 1E6 int array is too big, do you?

RL
You shouldn't have any problems with 1,000,000 element int array.

Oct 1 '08 #4
raylopez99 <ra********@yahoo.comwrote:
I took this to mean: sizeof(int) * 4 * N, where N= array elements?
And sizeof(int) is two bytes on most machines?
No, 4 * N + overhead, where N = array elements.

That's because 4 = sizeof(int) regardless of machine.
So, with the overhead, 8 to 12 bytes per element * a 1E6 element array
(million element array) is 12 M bytes?
No, I didn't say 8 to 12 bytes per element.

So a million element array will take about 4,000,012 bytes - ~4MB.

Basically arrays take up as much memory as you'd want them too -
they're not wasteful.
BTW, what's the biggest array you've had in C# in memory? I have two
gigs RAM and I don't think a 1E6 int array is too big, do you?
No, a million int array isn't huge. I've no idea what the most I've had
is for a production app.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 1 '08 #5

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

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
22
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
1
by: mccoyn | last post by:
I have a macro that can determine the number of elements in an array type. It is defined as follows #define ARRAY_SIZE_NOGC(x) (sizeof(x) / sizeof(x) This works with unmanaged array types, but...
20
by: Fred Hebert | last post by:
I am trying to learn VC.NET and convert some Borland C++ applications. The syntax differences are killing me... Is there an easy way to convert a hex string, entered by the user, to a binary...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
37
by: Greg | last post by:
Except for legacy or non-.NET applications, is there any reason to use VC++ anymore? It seems that for .NET applications, there would be no reason to choose C++ over C# since C# is faster to...
55
by: Robotnik | last post by:
Hello All, I want to know if we could know the size of a structyure without the use of sizeof(). Any hints.
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
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...

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.