Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximal number of methods per .NET class

Title asks it all, actually, but still, for completeness sake:

Hi, I'm writing a small post-compiling tool in the .NET platform, and while trying to optimize it, I've encountered a question I can-not easily find an answer to from the ECMA standards for the Common Language Infrastructure (CLI).

What is the max # of methods a single class can have? Is there a limit?

Edit:

Thanks to Kelsey for pointing out to a real-life test. Although I still would care about what the actual limit is, for my actual real-life purposes, I wanted to know if it is 2^16 / 2^32 -or- 2^31-1, as he pointed out, it appears to be clearly above 64K methods per class..

like image 616
damageboy Avatar asked Jun 09 '11 15:06

damageboy


3 Answers

Interesting question but no clue why you would ever hit the limit in reality so the answer might not be that useful because it is a high number.

I found this thread where someone wrote the following test to actually create a class with increasing amounts of functions to see where the breaking point was:

namespace MethodCountLimitFinder
{
    class Program
    {
        [System.STAThreadAttribute]
        static void Main ( string [] args )
        {
            Microsoft.CSharp.CSharpCodeProvider provider = 
                new Microsoft.CSharp.CSharpCodeProvider() ;
            System.CodeDom.Compiler.CompilerParameters cp = 
                new System.CodeDom.Compiler.CompilerParameters() ;
            cp.GenerateExecutable = false ;
            cp.GenerateInMemory = true ; 
            System.CodeDom.Compiler.CompilerResults cr = null ;
            System.Text.StringBuilder inner = 
               new System.Text.StringBuilder ( "namespace Tester { class Test {" ) ;

            int methodCount = 1000000 ; 
            while ( true )
            {
                System.Console.WriteLine ( methodCount ) ;

                for ( int i = methodCount ; i > 0 ; i-- )
                {
                    inner.AppendFormat ( "void M{0}(){{}}\n" , methodCount++ ) ;
                }    
                inner.Append ( "}}" ) ;                
                cr = provider.CompileAssemblyFromSource ( cp , inner.ToString() ) ;
                if ( cr.Errors.Count > 0 )
                {
                    break;
                }                
                inner.Remove ( inner.Length - 2 , 2 ) ;
            } 
            foreach (  System.CodeDom.Compiler.CompilerError ce in cr.Errors )
            {
                System.Console.WriteLine ( ce.ToString() ) ;
            }
        }
    }
}

Based on the results it looks like it is resource dependent, not the spec which is most likely not hard defined unless you tie it to a 32/64-bit index reference or something which I don't think is realistic since you will hit a resource limit probably first anyways.

The test got over 200k+ before failing due to lack of resources.

Again, interesting but not all that useful of information IMO.

like image 67
Kelsey Avatar answered Nov 03 '22 07:11

Kelsey


I think you'll need to look through the unmanaged interfaces into the CLR for definitions of the metadata structures to find limits on the size of those structures. This answer (and comments) already notes this is ~16 million (24bit index) methods per assembly (not type).

Alternatively consider that reflection methods return arrays, and there is a limit of 1GB per object in the current .NET runtime, so the maximum would be 1024^3/sizeof(MethodIndo).

like image 3
Richard Avatar answered Nov 03 '22 06:11

Richard


Found few links which may be helpful for you.

Limit For Java is 65535

Similar Question in SO for .NET

Similar Post

God Object

like image 2
Rahul Avatar answered Nov 03 '22 06:11

Rahul