Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do methods affect the size of my objects?

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.

like image 347
Rick Avatar asked Feb 11 '09 16:02

Rick


2 Answers

No. Methods take up memory but that's very little. The method table as well as the code generated for the method is shared by every instance of the object.

like image 90
Hans Passant Avatar answered Nov 14 '22 21:11

Hans Passant


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.

like image 24
bruno conde Avatar answered Nov 14 '22 23:11

bruno conde