Objectlists and memory

P

Peter Hartlén

Hi!

The basic questions here is how an object is stored in memory.

I have an list of a custom object, where the object has a couple of fields,
properties and methods. If we pretend that he fields take 24bytes, the
properties take 12bytes and the methods take 36bytes of code for this
particular object, will the lists memory consumption be (24+12+36)*number of
objects in list ?

I understand that you have to store the field data for each object, as it
probably differs, but how about properties and methods?

Especially method looks the same regardless of what object we are looking at
(I may be mistaking here), so why store the method memory for each object in
a list? Or am I talking STATIC methods here?

Why I am asking is that I am playing around with business layer objects, and
these objects don't have a lot of fields, but the code for the methods might
be quite long. So if the list of these objects is quite long it would
contain a lot of redundant code of the same method code.

Oh well, any comments are welcome :)

/ Peter
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Peter Hartlén said:
Hi!

The basic questions here is how an object is stored in memory.

Your first step should be http://www.yoda.arachsys.com/csharp/memory.html
I have an list of a custom object, where the object has a couple of
fields, properties and methods. If we pretend that he fields take 24bytes,

In any case the references takes that, unless all your fields are
primitive values types (int, char, long, etc)
If you have a reference type (string , object ) you cannot be sure how big
they are.
properties take 12bytes and the methods take 36bytes of code for this
particular object, will the lists memory consumption be (24+12+36)*number
of objects in list ?

Not at all, the code exist only once.
I understand that you have to store the field data for each object, as it
probably differs, but how about properties and methods?

They exist only once, and not in the same place than the data !
 
L

Laura T.

The size is, 1*(12+36)+n*(24+SIZEOF(Object)+system
overhead)=(code)+n*(data+parent objects data+system header data).

That is, the methods and properties (I assume you mean accessors to fields)
are shared between all the instances of the same class. Only data is
different so when you create a new MyObject the system creates only a data
container.

Laura
 

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