Thursday, April 28, 2011

Instantiation of objects

If I use multiple instances of the same object in my code, do the instance methods for each separate object require memory, or do all of those objects share the same instance methods?

It could have a big effect on memory use.

Thanks in advance for any insights.

John Doner

From stackoverflow
  • It might depend on the language you are using ;-)

    In all language implementations that I'm aware of, the methods are shared. In most C++ implementations, for example, the method gets an implicit first parameter (this) that points to the particular instance of the object.

  • You didn't specify what language you're talking about. But, in general, the separate instance methods will not take up more memory.

    To tell you the truth, you probably shouldn't even think about these details, as modern language designers have thought about this for you, and they have probably chosen the best thing. So, if there's no real reason to keep another copy (because all of the methods are actually the same), they won't save another copy.


Post a Comment