11

Just short question, if you have a class with just one 1 property, and lots of (non static) methods, does an entirely new object get stored every time you say 'new object()', or just the property, and the methods in some 'common' memory space so the same Type can reference to that?

Thus, is having a large class always performing worse than a small class in terms of instantiation time?

1
  • 1
    please also check the EDIT I just made. I really don't know much about memory allocation but.. I wanted to emphasize on the difference between time consumption and memory space consumption Commented Sep 11, 2013 at 15:16

4 Answers 4

4

Memory allocation may prove to be time consuming indeed. Still, I believe a cleaner, more obvious measurement of resource consumption would be occupied space not instantiation time.

As you have stated yourself already, it is the case that methods, static or not, occupy memory space just once. The this reference is just a hidden parameter, which gets sent from caller to called code just like any other parameter and in the end, all methods are just plain ol' functions (or routines).

In a simplistic way of putting it, so do all static fields. Don't think about properties. They are just high level wrappers for methods which in the end access fields.

Instance fields are what occupies space, per instance. But there are other things, like runtime type information which get allocated also.

In short, your assumption is correct.

EDIT

Just as a recap:

  • If by "large class" you mean a class which defines a lot of methods, then no, instantiation time will not be affected.
  • On the other hand, if by that term, you mean a class which defines a lot of instance fields, then yeah, instantiation time will be affected.

Although this is not my happy place (I know almost nothing of how good ol' malloc actually manages to defragment memory) thinking that allocating a lot of memory would take a longer time is in a strange way I can't put my finger on, like saying that

"adding the numbers 1024 and 2048 takes a bit longer than adding the numbers 3 and 4"

(given all 4 numbers are stored in variables of same numerical type).

So I would worry more about memory consumption. I'm sure time is somehow affected too, but maybe logarithmically.

0
4

Methods are shared. All other things being equal, instantiating a class with many methods has pretty much the same cost as instantiating one with few. It's their non-static fields and the amount of work performed by the constructor (and some other minor factors) that determine creation cost.

4
  • 2
    Properties are just a pair of methods, usually with a backing field. This field is the only part of a property that's actually stored in the object. It's just like an ordinary field. Commented Sep 11, 2013 at 15:08
  • properties are not the opposite of non-static fields. Btw: properties can be static or not themselves. Commented Sep 11, 2013 at 15:09
  • you're right, I need to stop using properties in this context since its usually just a getter + setter. Fields (or variables I was thought) is better. Commented Sep 11, 2013 at 15:12
  • @user2713516 Fields are sometimes also called member variables.
    – svick
    Commented Sep 11, 2013 at 15:24
3

Instance fields are the only thing stored in the object itself. Methods are stored in the type, which means that they only exist in one place.

In fact, instance methods are just syntactic sugar (at the IL level) for static methods that accept an instance as a parameter.

1
  • Non-virtual instance methods could be considered just a syntactic sugar in IL. But virtual methods (and interface methods) behave differently.
    – svick
    Commented Sep 11, 2013 at 15:25
0

I think you will find the information you need (and probably more) here . The code of the instance methods will be shared.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.