What is the maximum possible length of a .NET string?

后端 未结 8 1767
梦毁少年i
梦毁少年i 2020-11-22 02:48

What is the longest string that can be created in .NET? The docs for the String class are silent on this question as far as I can see, so an authoritative answe

8条回答
  •  一个人的身影
    2020-11-22 03:28

    The max length of a string on my machine is 1,073,741,791.

    You see, Strings aren't limited by integer as is commonly believed.

    Memory restrictions aside, Strings cannot have more than 230 (1,073,741,824) characters, since a 2GB limit is imposed by the Microsoft CLR (Common Language Runtime). 33 more than my computer allowed.

    Now, here's something you're welcome to try yourself.

    Create a new C# console app in Visual Studio and then copy/paste the main method here:

    static void Main(string[] args)
    {
        Console.WriteLine("String test, by Nicholas John Joseph Taylor");
    
        Console.WriteLine("\nTheoretically, C# should support a string of int.MaxValue, but we run out of memory before then.");
    
        Console.WriteLine("\nThis is a quickish test to narrow down results to find the max supported length of a string.");
    
        Console.WriteLine("\nThe test starts ...now:\n");
    
        int Length = 0;
    
        string s = "";
    
        int Increment = 1000000000; // We know that s string with the length of 1000000000 causes an out of memory exception.
    
        LoopPoint:
    
        // Make a string appendage the length of the value of Increment
    
        StringBuilder StringAppendage = new StringBuilder();
    
        for (int CharacterPosition = 0; CharacterPosition < Increment; CharacterPosition++)
        {
            StringAppendage.Append("0");
    
        }
    
        // Repeatedly append string appendage until an out of memory exception is thrown.
    
        try
        {
            if (Increment > 0)
                while (Length < int.MaxValue)
                {
                    Length += Increment;
    
                    s += StringAppendage.ToString(); // Append string appendage the length of the value of Increment
    
                    Console.WriteLine("s.Length = " + s.Length + " at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
    
                }
    
        }
        catch (OutOfMemoryException ex) // Note: Any other exception will crash the program.
        {
            Console.WriteLine("\n" + ex.Message + " at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + ".");
    
            Length -= Increment;
    
            Increment /= 10;
    
            Console.WriteLine("After decimation, the value of Increment is " + Increment + ".");
    
        }
        catch (Exception ex2)
        {
            Console.WriteLine("\n" + ex2.Message + " at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + ".");
    
            Console.WriteLine("Press a key to continue...");
    
            Console.ReadKey();
    
        }
    
        if (Increment > 0)
        {
            goto LoopPoint;
    
        }
    
        Console.WriteLine("Test complete.");
    
        Console.WriteLine("\nThe max length of a string is " + s.Length + ".");
    
        Console.WriteLine("\nPress any key to continue.");
    
        Console.ReadKey();
    
    }
    

    My results were as follows:

    String test, by Nicholas John Joseph Taylor

    Theoretically, C# should support a string of int.MaxValue, but we run out of memory before then.

    This is a quickish test to narrow down results to find the max supported length of a string.

    The test starts ...now:

    s.Length = 1000000000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 100000000.

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 10000000. s.Length = 1010000000 at 08/05/2019 12:06 s.Length = 1020000000 at 08/05/2019 12:06 s.Length = 1030000000 at 08/05/2019 12:06 s.Length = 1040000000 at 08/05/2019 12:06 s.Length = 1050000000 at 08/05/2019 12:06 s.Length = 1060000000 at 08/05/2019 12:06 s.Length = 1070000000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 1000000. s.Length = 1071000000 at 08/05/2019 12:06 s.Length = 1072000000 at 08/05/2019 12:06 s.Length = 1073000000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 100000. s.Length = 1073100000 at 08/05/2019 12:06 s.Length = 1073200000 at 08/05/2019 12:06 s.Length = 1073300000 at 08/05/2019 12:06 s.Length = 1073400000 at 08/05/2019 12:06 s.Length = 1073500000 at 08/05/2019 12:06 s.Length = 1073600000 at 08/05/2019 12:06 s.Length = 1073700000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 10000. s.Length = 1073710000 at 08/05/2019 12:06 s.Length = 1073720000 at 08/05/2019 12:06 s.Length = 1073730000 at 08/05/2019 12:06 s.Length = 1073740000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 1000. s.Length = 1073741000 at 08/05/2019 12:06

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:06. After decimation, the value of Increment is 100. s.Length = 1073741100 at 08/05/2019 12:06 s.Length = 1073741200 at 08/05/2019 12:06 s.Length = 1073741300 at 08/05/2019 12:07 s.Length = 1073741400 at 08/05/2019 12:07 s.Length = 1073741500 at 08/05/2019 12:07 s.Length = 1073741600 at 08/05/2019 12:07 s.Length = 1073741700 at 08/05/2019 12:07

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:07. After decimation, the value of Increment is 10. s.Length = 1073741710 at 08/05/2019 12:07 s.Length = 1073741720 at 08/05/2019 12:07 s.Length = 1073741730 at 08/05/2019 12:07 s.Length = 1073741740 at 08/05/2019 12:07 s.Length = 1073741750 at 08/05/2019 12:07 s.Length = 1073741760 at 08/05/2019 12:07 s.Length = 1073741770 at 08/05/2019 12:07 s.Length = 1073741780 at 08/05/2019 12:07 s.Length = 1073741790 at 08/05/2019 12:07

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:07. After decimation, the value of Increment is 1. s.Length = 1073741791 at 08/05/2019 12:07

    Exception of type 'System.OutOfMemoryException' was thrown. at 08/05/2019 12:07. After decimation, the value of Increment is 0. Test complete.

    The max length of a string is 1073741791.

    Press any key to continue.

    The max length of a string on my machine is 1073741791.

    I'd appreciate it very much if people could post their results as a comment below.

    It will be interesting to learn if people get the same or different results.

提交回复
热议问题