views:

139

answers:

2

In .NET, do the number of methods or the size of the methods (i.e., amount of code) within an object affect the amount of memory the object uses when it is instantiated?

EXAMPLE: Will an object with 3 int properties and 1 method take up more memory than an object with 3 int properties and 20 methods?

If "yes", do static methods take up less memory?

Please note: I realise that actually calling a method might instantiate other objects or variables that will use memory - my question simply relates to how much memory they use after calling the constructor.

+7  A: 

No. Virtual methods take up memory but that's very little. The method table is shared by every instance of the object.

Hans Passant
So methods do take up memory, but it is shared between objects of the same type? (i.e., The "method memory" use for 1 object is the same as the "method memory" use of 100 objects of the same type?)
Rick
yes. That's correct
bruno conde
+2  A: 

Will an object with 3 int properties and 1 method take up more memory than an object with 3 int properties and 20 methods?

The answer is no. Methods aren't part of any particular instance. They belong to the type.

bruno conde