Re: There is no sizeof in a managed language, right?



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
.



Relevant Pages

  • Re: "Parallel.For GC problems" and a solution.
    ... I think Dictionary(of dictionary(of small array of ints)) ... with the dictionaries holding arrays of keys, ... do not pass it a large array of things to process. ... With no breaks, memory goes crazy. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: contiguity of arrays
    ... >> What you seem to be saying is that for a region of memory to constitute ... >> an array of four ints, it doesn't have to be declared with a type that ... A struct type that consists of four ints ...
    (comp.lang.c)
  • Re: Reclaiming Memory
    ... example, handles to device context, memory addresses, etc.), VB is actually ... VB will clean up references to objects all ... It's a little unusual to use an array to store object references. ... unusual to use a variant array for what you apparently are (at least I'VE ...
    (microsoft.public.vb.com)
  • Re: contiguity of arrays
    ... >> What you seem to be saying is that for a region of memory to ... >> a type that involves an array of four ints, ... > of type T. The memory allocation functions are a special case that return ...
    (comp.lang.c)
  • Re: Set / List
    ... Less memory use. ... A hash has an array where it stores Entries, ... Each entry has at least two references. ...
    (comp.lang.java.programmer)

Quantcast