8

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

damageboy
  • 2,097
  • 19
  • 34
  • 1
    What are you doing where it's important to know that? – Sven Jun 09 '11 at 15:30
  • 5
    If you hit that maximum then you are doing it wrong! – Colin Mackay Jun 09 '11 at 15:31
  • 4
    I don’t agree with @Colin’s assessment. Given that the OP is writing a post-compilation processing tool (?) this is actually a reasonable question, and depending on the exact problem an optimization where the number of methods is stored internally as a `short` or `byte` instead of an `int` (say) may make sense (of course, this will be the absolute exception but let’s give the OP the benefit of doubt). – Konrad Rudolph Jun 09 '11 at 15:35
  • 2
    @Colin: even if it is generated code? Who *cares* then? – jalf Jun 09 '11 at 15:36
  • IMO it seems ridiculous to place a limit. OOP is about modelling behaviour generically and completely. A limit in the number of methods would undermine these properties of OOP so in theory one could justify any number of methods. – Arj Jun 09 '11 at 15:41
  • Thanks for the vote of cofidence @Konrad... Basically it's exactly whay you said. I have a master list of methods and I want to create a partial lookup function on top of that list. the key for that partial lookup would be the method name, and I'd like to basically create a dictionary from method name -> (start-index, count), where each entry in the dictionary points to a start position on the master list and a count of how many methods have the same name (naturally the master list has to be sorted by the method name)... Hope this clarified why I'm supposedly wasting everybody's time... – damageboy Jun 09 '11 at 16:04

3 Answers3

7

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.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • This could well be hitting limits with the number of (dynamic) assemblies in an AppDomain (or modules in a Win32 process) before coming anywhere close to the limit of methods per class or assembly. – Richard Jun 09 '11 at 15:53
  • The reason I even care, is because I'm trying to create a quick lookup structure where I basically need to store as compactly as possible an index to an array of methods and a count. If I could assume that both are 16 bits (had the method limit been 16 bits) I could just pack both of them into one 32 bit value (i.e. startIdx | count << 16) – damageboy Jun 09 '11 at 16:00
3

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

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
2

Found few links which may be helpful for you.

Limit For Java is 65535

Similar Question in SO for .NET

Similar Post

God Object

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125