P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

How are methods referenced in memory?

In C# I understand that static variables are created in memory and live as long as the AppDomain lives. How do static methods get treated in memory? Do methods get created on the stack/heap like variables? Is there such a thing as method GC? How does this relate to the Call Stack, if it all?

Upvotes: 1

Views: 94

Answers (2)

Guffa
Guffa

Reputation: 700800

Methods live in the code segment, neither in the heap or the stack. The code is just loaded into memory, and stays there as long as the AppDomain lives.

Static methods and non-virtual methods just have an address, and the code uses the address directly when calling the method.

Virtual methods also have an address, but it's not used directly. There is a pointer to the method in the virtual method table that is associated to the class, so the code gets the pointer from the table to find the method.

The call stack is just another term for the stack.

Upvotes: 3

zmbq
zmbq

Reputation: 39059

Methods aren't located on the heap, they're part of your code - static or instance methods for that matter.

Upvotes: 0

Related Questions