How instance of the same class share the code

  • Thread starter Thread starter Ashish Sheth
  • Start date Start date
A

Ashish Sheth

Hi gurus,
I have this question from last so many years and I tried to find out but
couldn't find anything.
Basically suppose one class is having two integer member variables and one
method. My question is How much memory will be consumed by each instance of
the class? OK I know that since there are two integer member variable it
will consume 2* 4 bytes but what about the Method code? will the method code
will be shared by the two different instance or the method code be copied
for the all the instances? If it is not copied(i.e. shared) then How the
method will be encapsulated so that only instance of that class only can
call the method? the final question is Does the memory allocated by the
object comprises the memory taken by the method code(or method pointer or
whatever) ?


thanks and regards,
Ashish Sheth.
 
Ashish Sheth said:
I have this question from last so many years and I tried to find out but
couldn't find anything.
Basically suppose one class is having two integer member variables and one
method. My question is How much memory will be consumed by each instance of
the class? OK I know that since there are two integer member variable it
will consume 2* 4 bytes but what about the Method code? will the method code
will be shared by the two different instance or the method code be copied
for the all the instances? If it is not copied(i.e. shared) then How the
method will be encapsulated so that only instance of that class only can
call the method? the final question is Does the memory allocated by the
object comprises the memory taken by the method code(or method pointer or
whatever) ?

Method code effectively belongs to the type itself, not individual
instances.

I'm not sure what you mean with your last two questions, I'm afraid.
 
Oversimplified explanation: When applications are loaded, they endup in two distinct process spaces, one is called the "code" space and its readonly and then the data where things can be modified.
Depending on how individual compilers/JIT compilers do it, it can be done at compile time or at run time. An way, so, that means the code for a class can and will exist in one place only.
 
Hi, Ashish.
As mentioned in all previous posts, method code doesn't consume any object
memory.
How is this possible? For a minute, let's assume that there is NO member
methods, all methods are either static or global (.net doesn't support
global). Objects only contain data. It's obvious that static methods don't
consume object memory --- they belong to the type/class:

public class MyTwoInt {
int i, j;
}

If you want to implement a method (either static or global) that sums i
and j and returns the result, it could look like this:
public static int Sum(MyTwoInt this) { //pretend "this" is not a
keyword
return this.i + this.j;
}

And it would be invoked this way:
MyTwoInt mti = new MyTwoInt(); //allocate only 8 bytes.
MyTwoInt.Sum(mti);

On the other hand, if we twist the syntax of both definition and method
call a little bit:
public class MyTwoInt {
//make the first parameter implied type of MyTwoInt and name of
"this".
public int Sum() {
return this.i + this.j;
}
...
}

MyTwoInt mti = new MyTwoInt(); //allocate only 8 bytes.
mti.Sum(); //make the object before "." the first
paraemter.

As long as the compiler (in the case of .NET, the JIT compiler) is capable
of translate above code into the original form (static method + data
object), any object of MyTwoInt type still only consumes 8 bytes. But now we
have the notion of "member method" that "belongs" to the object, not type.
:) There is still just one copy of method code in memory.

This explanation is oversimplified, however. It doesn't consider virtual
function, inheritence, etc. For a thorough explanation of the matter, you
can check out "Inside C++ Object Model" by Stanley Lippman. I'm not aware of
any book that explains the .NET implementation. The implementations of C++
and .NET are very similar.

Hope this helps.
Ming Chen
 
Back
Top