在。net中可以创建的最长字符串是什么?据我所知,String类的文档对这个问题保持沉默,所以权威的答案可能需要一些内部知识。64位系统的最大变化是多少?
[这个问题更多的是出于好奇而不是实际用途——我不打算创建任何使用巨大字符串的代码!]
在。net中可以创建的最长字符串是什么?据我所知,String类的文档对这个问题保持沉默,所以权威的答案可能需要一些内部知识。64位系统的最大变化是多少?
[这个问题更多的是出于好奇而不是实际用途——我不打算创建任何使用巨大字符串的代码!]
当前回答
理论上的极限可能是2147,483,647,但实际的极限远不及此。由于. net程序中的单个对象不可能超过2GB,并且字符串类型使用UTF-16(每个字符2字节),所以您最多可以做到1,073,741,823,但是您不太可能在32位计算机上分配它。
这是一种“如果你必须问,你可能做错了什么”的情况。
其他回答
在我的机器上,字符串的最大长度是1,073,741,791。
你看,字符串并不像通常认为的那样受整数限制。
除了内存限制外,字符串不能超过230(1,073,741,824)个字符,因为Microsoft CLR(公共语言运行时)施加了2GB的限制。比我电脑允许的多33个。
现在,这里有一些东西欢迎你自己尝试。
在Visual Studio中创建一个新的c#控制台应用程序,然后复制/粘贴main方法:
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();
}
我的研究结果如下:
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.
我的机器上字符串的最大长度是1073741791。
如果大家能在下面留言,我会非常感激的。
如果人们得到相同或不同的结果,这将是很有趣的。
理论上的极限可能是2147,483,647,但实际的极限远不及此。由于. net程序中的单个对象不可能超过2GB,并且字符串类型使用UTF-16(每个字符2字节),所以您最多可以做到1,073,741,823,但是您不太可能在32位计算机上分配它。
这是一种“如果你必须问,你可能做错了什么”的情况。
因为字符串。Length是一个整数(这是Int32的别名),其大小限制为Int32。MaxValue unicode字符。: -)
200 mb……这时,你的应用程序会陷入虚拟停顿,只有大约gig的工作集内存,o/s开始表现得好像你需要重新启动。
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
由于系统的长度属性。字符串是Int32,我猜最大长度将是2147,483,647个字符(最大Int32大小)。如果它允许更长的时间,你就不能检查长度,因为那会失败。