Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are methods stored in memory?

Tags:

c#

.net

memory

I learned that class fields are stored in the heap, but where are methods stored? In the heap or somewhere else? are they inline?

like image 546
Prashant Cholachagudda Avatar asked Aug 19 '09 06:08

Prashant Cholachagudda


People also ask

Where methods are stored in memory in Java?

Static information (interface & class boxes) and instance information (object boxes) are stored in the heap. Method information is stored in the run-time stack.

Are methods stored in heap or stack?

Methods and variables(inside methods) are stored in the stack. Objects and Instance variables are stored inside the heap. When an object is called inside a stack method it has a pointer to the heap object.

Where does a method live?

The "Method" "Lives" in the code space. It is not on the stack or the heap. An object is created on the heap that has a pointer for each of its methods. That object lives in the heap, but the methods it points to are in code.


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".

like image 67
Martin v. Löwis Avatar answered Sep 17 '22 14:09

Martin v. Löwis


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.

like image 38
sharptooth Avatar answered Sep 19 '22 14:09

sharptooth