I have been trying to learn about the memory management in C# and it is a very interesting topic. I made one simple code which is as follows:
using System; public class MyClass { public void Go() { int x = 5; AddFive(x); } public int AddFive(int pValue) { pValue += 5; Console.WriteLine(pValue); return pValue; } public static void Main(String[] args) { MyClass mc1 = new MyClass(); mc1.Go(); Console.ReadLine(); } }
I was reading an article from this website about the memory management by Matthew Cochran and it had the following statements:
When we make a method call here's what happens:
- Space is allocated for information needed for the execution of our method on the stack (called a Stack Frame). This includes the calling address (a pointer) which is basically a GOTO instruction so when the thread finishes running our method it knows where to go back to in order to continue execution.
- Our method parameters are copied over. This is what we want to look at more closely.
- Control is passed to the JIT'ted method and the thread starts executing code. Hence, we have another method represented by a stack frame on the "call stack".
Please explain me all the three points(especially the first and the third point) in a very simple way. I am a beginner and i want to know what happens when we execute a method..... Thanks in advance!!!

Sam HobbsPosted Jul 9, 2011, 2:05 PM
The best way to learn about stacks is to learn assembler; that is, the language that corresponds with the processor's machine language. I have. I have programmed in assembler for big IBM "mainframe" type computers and for 80386 processors which is an earlier version of Intel Pentium (x86) processors, but IBM "mainframe" type computers don't use stacks.
I doubt that "calling address (a pointer) which is basically a GOTO instruction" is accurate. It is close enough for a beginner's understanding but it is only an address that is put in the stack, not an entire machine instruction.
In a x86 type of processor (and most but not all processors) there are a few very special peices of memory within the processor called registers. A pair of them are for the stack; one points to the (base of the) stack and the other points to the current offset. When stuff is put onto the stack, the offset is increased and when stuff is taken from the stack the offset is decreased. The stack is used to keep track of where to return to when a function returns. The stack is also where the parameters are put for functions that are called. The stack is also where C# "value" (struct) types are put. The stack must be contiguous. Stack space is allocated/deallocated very efficiently since it is just a matter of incrementing/decrementing the register and the register is accessed very efficiently. The stack is a low-level thing that you generally do not need to be concerned about except it really helps to understand that it is a limited resource and therefore it's size is significantly less than available memory. There are many other things to learn; very many that are more important than the stack.
VulpesPosted Jul 9, 2011, 1:48 PM