Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When I create an instance of a class the system allocates memory for all instance variables of the class. Some languages may call them fields or properties.

But what about the code of the class methods? Are those duplicated too or do I have the code only once in memory? I know all instances got their own values - even inside the methods - but I would like to know about the code.

Do I have instance variables and separate "instance methods"?

share|improve this question

closed as too broad by gbjbaanb, GlenH7, MichaelT, gnat, DeadMG Aug 30 at 21:59

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
This depends not just on the programming language but in most cases also on its implementation. You need to be more specific. –  Philipp Aug 28 at 18:58
2  
@Philipp Far less so than with many other things. In fact, I can't think of a single serious language implementation that duplicates the methods' code, nor for a reason why one might want to. –  delnan Aug 28 at 18:59
    
Javascript allows you to modify methods for individual instances. I don't know the details of how it is implemented. –  Cerad Aug 28 at 20:47
    
@delnan: Lua makes it easy to accidentally give each instance it's own copy of the type's methods. –  Mooing Duck Aug 28 at 21:56
    
@delnan: Indeed, javascript, just like Lua, may also end up making copies of methods. Depending on how that method was defined. You have to remember that methods in javascript are just functions (generally anonymous functions) and functions are just closures. Therefore the programmer may WANT to have the class make new copies of a method for each instance of an object. –  slebetman Aug 29 at 1:25

5 Answers 5

In layman's terms:

  • Methods are not part of the "state" of an object
  • They are part of the blueprint
  • They are part of the cookie cutter, not the dough
  • They belong to the class, not the instance
  • Hence I guess they are stored in memory once
share|improve this answer
    
In some languages methods may be used to implement the state of objects. For a lot of functional languages this is idiomatic. –  slebetman Aug 29 at 1:26
    
@slebetman That's true. But the question is tagged as object oriented. –  user61852 Aug 29 at 1:41
    
Yes, I meant object oriented functional languages. Like Lisp or Tcl or Javascript. –  slebetman Aug 29 at 11:38

Do I have instance variables and separate "instance methods"?

Generally no. Methods belong to the class itself and look like C# extension methods under the hood:

void Foo(this T instance, param, param, param)

so that x.Foo(a,b,c) really gets called like Foo(x,a,b,c). That said, certain languages/implementations allow replacing the methods, so can allocate space for a pointer to which Foo to use, but even in those cases the language makes those methods look like variables (since they effectively are variables).

share|improve this answer
    
In some languages methods may carry with it a closure. Therefore the language allow the programmer to assign new instances of the method so that each object carry with it the right closure (for example methods assigned in the constructor as this.method = function () ... in javascript). –  slebetman Aug 29 at 1:28
    
In the case of methods carrying a closure, the closure should be given as a parameter to the actual code of the method, just like the object data is given as a parameter to the implmentation of an instance method. There is no reason to duplicate the code. –  Florian F Aug 30 at 16:37

Code is part of the static data associated with the class.

Typically eEach class has a function table that references the virtual (aka. overridable) functions and each object has a pointer to the function table of its instance class. So the collection of instance methods are coalesced into a single pointer in the object.

So when calling a method the compiler looks into the table of the object and finds the correct function.

share|improve this answer

The compiler might inline your code, but the copy that makes is not passed around with the instance. The copy is physically stored within the code of the call site. Likewise templates create a duplicate copy (except for the substituted types) for each different type of template instantiated, but those are not passed around with the instance either.

Under certain circumstances the instance's memory will contain a function pointer, but I don't know any language implementations that store the actual instructions with the instance. Even in languages where functions are first-class and conceptually you are doing something like returning a closure from another function, the executable instructions are stored statically, and you're just passing around a pointer to those instructions and whatever state you need in context.

share|improve this answer

This cannot be objectively answered without knowing the language.

Here are explanations for two languages which cover many of the generally important points.

C++

Typically, some code will be duplicated while other code will not. Inline functions will of course be duplicated, and this includes any code in the class declaration which is implicitly inline. Code that is defined separately will not be duplicated.

Template classes (e.g. STL containers) are normally duplicated since they normally have the declaration and definition combined (see previous point).

Linkers can perform their own optimizations to combine code like this, although in practice I do not believe this is common.

Without a JVM or CLR to monkey around with memory at run time, whatever is compiled is highly likely to be what is loaded in memory at run time.

Java

Byte code will not be duplicated (i.e. in .class files). Due to the nature of class files being able to be compiled and manipulated separately, there will be no duplication or optimization across classes. However, the JIT compiler is free to inline code at runtime, which would duplicate it in memory.

Sources: Java HotSpot VM Options - JRockit (there are quite a few references but very little official Oracle documentation on this topic)

share|improve this answer
    
Do you have any references for Java part of your question? I'm not sure how inlining has anything to do with the question. –  MainMa Aug 28 at 19:42
1  
if a method is inlined, it will not be stored only once in memory. I will dig up the reference for the JIT part later when I have time. –  Snowman Aug 28 at 20:59
    
I added some references. There are quite a few out there, but very little documentation to serve as a primary reference. –  Snowman Aug 28 at 21:19
    
@MainMa: Copying the method's code into the calling method is pretty much the definition of "inlining". So, yes, as soon as you have inlining, you have duplicate copies of the callee method. And pretty much every serious language implementation does perform inlining, because that is one of the most important and most basic optimizations. It does not just remove the dispatch and call overhead, it more importantly gives the compiler a nice big chunk of straight-line code with no jumps and no calls, which is what optimizers love. –  Jörg W Mittag Aug 28 at 22:17
    
@JörgWMittag: I know what inlining is. I just don't understand how is that related to the question, which is about the existence or not of the duplication of methods within every instance of a class. –  MainMa Aug 28 at 23:13

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