8

I have a some small doubt with classes and objects.

In a class I have 10 to 20 methods, (shared below)

    public class tstCls
    {
       public void a1()
        { }
        void a2()
        { }
        void a3()
        { }
        void a4()
        { }
        void a5()
        { }
        void a6()
        { }
        void a7()
        { }
        void a8()
        { }
        void a9()
        { }
    }

When creating a object for the above class. How many methods will be stored in memory? Only the calling method or all the methods.

 static void Main(string[] args)
    {
        tstCls objcls = new tstCls();
        objcls.a1();
    }

Can you please help me on the above scenario.

Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
Anjali
  • 1,680
  • 4
  • 26
  • 48

3 Answers3

5

None.

The methods are not created in memory when you instantiate an object, only the fields and properties are.

The assembly that contains the methods is loaded whenever any part of the assembly is referenced.

Methods get compiled into memory only when they are called. The JIT (Just-In-Time compiler) turns the methods from IL into machine code at that moment in time.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

For an instance only one method will be created.

You can refer this artilce: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects

Each class and interface, when loaded into an AppDomain, will be represented in memory by a MethodTable data structure. This is a result of the class-loading activity before the first instance of the object is ever created. While ObjectInstance represents the state, MethodTable represents the behavior. MethodTable binds the object instance to the language compiler-generated memory-mapped metadata structures through EEClass. The information in the MethodTable and the data structures hanging off it can be accessed from managed code through System.Type. A pointer to the MethodTable can be acquired even in managed code through the Type.RuntimeTypeHandle property. TypeHandle, which is contained in the ObjectInstance, points to an offset from the beginning of the MethodTable. This offset is 12 bytes by default and contains GC information which we will not discuss here.

enter image description here

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Note that this diagram is slightly outdated, for instance IVMap tables were replaced with [VSD](https://github.com/dotnet/coreclr/blob/master/Documentation/botr/virtual-stub-dispatch.md). For the latest info about JIT internals see [here](https://github.com/dotnet/coreclr/blob/master/Documentation/botr/ryujit-overview.md). – Lucas Trzesniewski Aug 12 '16 at 12:00
0

how many methods will store in the memory

All. All methods will be stored in memory.
Which is more important, they will all be created once, regardless of number of instances.

So, this statement is not precise: "while created an object for the above class...".

Every assembly type creates one method in memory - methods are not created / copied for every instance.
So, during assembly load all methods will be created and stored in memory, and then only a1 will be called for objcls instance.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • 1
    Strictly speaking, it might not be _all_ methods. Xamarin for example has a linker that can remove methods that aren't used: https://developer.xamarin.com/guides/ios/advanced_topics/linker/ – Ian Mercer Aug 12 '16 at 05:52
  • @IanMercer Thanks for your comment. What if linker removes "unused" method, and then this method is called through reflection? How would it also know that this assembly is not referenced by another library, and public method is not called? – Yeldar Kurmangaliyev Aug 12 '16 at 05:54
  • Take a look at the link, it explains that. For mobile devices you want the smallest possible application - using reflection is typically less important. It's really down to C# language (tag) vs. a specific implementation of it. The language doesn't say that all methods have to be compiled into an assembly. – Ian Mercer Aug 12 '16 at 05:59
  • @YeldarKurmangaliyev If you have checked out the link provided by IanMercer can you post you final outcome. I want to known that all the methods are stored in the memory or those which are used. – Parth Patel Aug 12 '16 at 06:10
  • There is no way to know exactly if a method is not used. Skipping a method in compilation seems dangerous for me in the case of reflection being used to call method dynamically ! – Zein Makki Aug 12 '16 at 06:10