I learned that class fields are stored in the heap, but where are methods stored? In the heap or somewhere else? are they inline?
2 Answers
Methods are stored somewhere else in the memory. Notice that methods are per-class, not per-instance. So typically, the number of methods doesn't change over the run-time of a program (there are exceptions). In traditional models, the place where the methods live is called the "code segment". In .net, it's more difficult: the methods originally live in the assembly, and get mapped into the process memory. There, the just-in-time compiler creates a second copy of some methods in native code; this copy gets executed. The JIT code may get created and deleted several times during the runtime, so it is practical to view it also as living "in Heap".

- 124,830
- 17
- 198
- 235
-
2I didn't say "pre-class", but "**per**-class". Methods are per-class: this means that every method exists only once, for the class. Different instances have still the same methods. Attributes/properties are per-instance: an attribute exists for every instance, i.e. different instances have different values for an attribute. – Martin v. Löwis Aug 19 '09 at 10:10
-
@Martin: i have one concern regarding this. I don't understand why JIT code created a copy every time that method executed. you already said that methods are Per-Class and i 100% agreed to that but i also believe that when ever a method is called it is referring the same actual method with out copying , because if there is some sort of copy thingy (that what you said) then how can method are Per-Class. – Singleton Nov 16 '10 at 10:17
-
3And what If I have a method that has 10,000 Lines of code and I create 1000 instances of the class to which the method belongs, are 1000 copies of the compiled code in that method created in memory or all 1000 objects are sharing same , both on execution of method and just Initialization of that objects? – Singleton Nov 16 '10 at 10:17
-
4@Hansmukh: I didn't say the JIT creates a copy every time that the method is executed. Instead, I said "several times during the runtime", meaning that the JIT may compile the method once, then discard it an hour later (if it doesn't get called for an hour), then recompile it two hours later, and so on. As for 1000 instances: notice that I said "methods are per-class"; I really mean that. That also holds for the JIT code; there will be at most one JIT version of any method at any point in time. – Martin v. Löwis Nov 19 '10 at 21:55
-
@Martin, your explanation is very good. but I am confused at one thing, if methods are per-class then what would happen if a method is "simultaneously" called by different objects of a class? will JIT create two different copies of that method? or will there be only one copy for both? – Zain Shaikh Nov 23 '10 at 10:38
-
and one more thing, with your thing explanation, I understood that there is no difference between instance methods and static methods, right? since JIT will at most have only one version of any method. – Zain Shaikh Nov 23 '10 at 10:53
-
8@Zain: To support simultaneous (or subsequent) activations of a method, "call stacks" are used, consisting of "stack frames". Every invocation of a method creates a new stack frame (in a memory region called the "stack"), consisting of all parameters to the method, plus any local variables. Different objects don't cause problems: "this" is just passed as parameter 0. – Martin v. Löwis Nov 23 '10 at 11:29
-
Another oddity the JiT can produce is inlining the functions. Functions calls have a slight overhead. Inlining is there to get around it: https://en.wikipedia.org/wiki/Inline_function And of course they told the JiT how to do that too. So it can be between 0 and multiple instances of that function (http://blogs.microsoft.co.il/sasha/2012/01/20/aggressive-inlining-in-the-clr-45-jit/). Tl;Dr: There are however many copy the CLR feels like having today. Wich of course changes by CLR version. – Christopher Feb 09 '18 at 16:55
Class methods are stored together with all code in a dedicated segment of program memory meant specifically for storing code. Each method's code is stored once.

- 167,383
- 100
- 513
- 979