views:

57

answers:

2

Hello

Does anyone know if and object's executable code/instructions that are stored in memory by the CLR when creating it are stored once for every instance of such object? Or they are duplicated for each created instance?

In other words, does the CLR stores once the Executable Instructions of an object and reuses that for every instance of it?

+2  A: 

The code is shared, as is the static data.

Only the parts of the class that are specific to an instance are kept separate.

lavinio
I wonder, are there any object oriented framework/language in the word where this is not so? :)
Bogdan_Ch
+1  A: 

Here's a little more detail, taken from http://msdn.microsoft.com/en-us/magazine/cc188793.aspx

"Whenever an object is created in the heap, each object gets two additional overhead fields associated with it. The first overhead field, the MethodTablePointer, contains the memory address to the type's method table. Basically, this pointer makes it possible to obtain the type information about any object in the heap. In fact, when you call System.Object's GetType method internally, this method follows the object's MethodTablePointer field to determine what type the object is."

So your instance doesn't replicate any of this shared type data/code. Instead, it has a pointer to it.

rice