views:

4431

answers:

5

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 answer might require some knowledge of internals. Would the maximum change on a 64-bit system?

[This is asked more for curiosity than for practical use - I don't intend to create any code that uses gigantic strings!]

+13  A: 

Since the Length property of System.String is an Int32, I would guess that that the maximum length would be 2,147,483,647 chars (max Int32 size). If it allowed longer you couldn't check the Length since that would fail.

Ryan Farley
+2  A: 

Since String.Length is an integer (which is an alias for Int32) its size is limited to Int32.MaxValue unicode characters ;)

VVS
+3  A: 

Based on my highly scientific and accurate experiment, it tops out on my machine well before 1,000,000,000 characters (I'm still running the below code to get a better pinpoint). UPDATE: After a few hours, I've given up. Final results: Can go a lot bigger than 100,000,000 characters, instantly given System.OutOfMemoryException at 1,000,000,000 characters.

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void Main()
    {
        int i = 100000000;
        try
        {
            for (i = i; i <= int.MaxValue; i+= 5000)
            {
                string value = new string('x', i);
                //WL(i);
            }
        }
        catch (Exception exc)
        {
            WL(i);
            WL(exc);
        }
        WL(i);
        RL();
    }

    #region Helper methods

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);   
    }

    private static void RL()
    {
        Console.ReadLine(); 
    }

    private static void Break() 
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}
bdukes
Applying a binary search here would probably help you find this answer a lot quicker...
Mario
+20  A: 

The theoretical limit may be 2,147,483,647, but the practical limit is nowhere near that. Since no single object in a .Net program may be over 2GB and the string type uses unicode (2 bytes for each character), the best you could do is 1,073,741,823, but you're not likely to ever be able to allocate that on a 32-bit machine.

This is one of those situations where "If you have to ask, you're probably doing something wrong."

HitScan
This is the correct answer. You're more likely to run out of memory before being able to allocate enough to exhaust the string length. On a fresh boot you might be able to pull an allocation of 2GB (with 1M characters) as mentioned here, but that's all.
Stephen Deken
Assuming that your "no single object may be over 2Gb" assertion is accurate, this IS the theoretical limit as well as the practical one - the constraint on String length would be the total object size, not the capacity of the Length field.
McKenzieG1
A: 

200 megs... at which point your app grinds to a virtual halt, has about a gig working set memory, and the o/s starts to act like you'll need to reboot.

static void Main(string[] args)
{
    string s = "hello world";
    for(;;)
    {
        s = s + s.Substring(0, s.Length/10);
        Console.WriteLine(s.Length);
    }
}

12
13
14
15
16
17
18
...
158905664
174796230
192275853
211503438
loudej