Re: There is no sizeof in a managed language, right?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 30 Sep 2008 19:51:52 +0100
raylopez99 <raylopez99@xxxxxxxxx> wrote:
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 - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.
- References:
- There is no sizeof in a managed language, right?
- From: raylopez99
- There is no sizeof in a managed language, right?
- Prev by Date: reflection's GetFields() not retrieving protected properties from base class
- Next by Date: Re: const and readonly
- Previous by thread: There is no sizeof in a managed language, right?
- Next by thread: reflection's GetFields() not retrieving protected properties from base class
- Index(es):
Relevant Pages
|