There is no sizeof in a managed language, right?

R

raylopez99

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
 
J

Jon Skeet [C# MVP]

raylopez99 said:
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).
 
R

raylopez99

Jon said:
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
 
B

Brian Gideon

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.
 
J

Jon Skeet [C# MVP]

raylopez99 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top