在。net中可以创建的最长字符串是什么?据我所知,String类的文档对这个问题保持沉默,所以权威的答案可能需要一些内部知识。64位系统的最大变化是多少?
[这个问题更多的是出于好奇而不是实际用途——我不打算创建任何使用巨大字符串的代码!]
在。net中可以创建的最长字符串是什么?据我所知,String类的文档对这个问题保持沉默,所以权威的答案可能需要一些内部知识。64位系统的最大变化是多少?
[这个问题更多的是出于好奇而不是实际用途——我不打算创建任何使用巨大字符串的代码!]
由于系统的长度属性。字符串是Int32,我猜最大长度将是2147,483,647个字符(最大Int32大小)。如果它允许更长的时间,你就不能检查长度,因为那会失败。
根据我高度科学和精确的实验,它在我的机器上达到了10亿个字符。(我仍在运行下面的代码以获得更好的精确位置)。
更新: 几个小时后,我放弃了。最终结果:能够在系统中创造出超过1亿个字符的内容。OutOfMemoryException在1,000,000,000个字符。
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
}
理论上的极限可能是2147,483,647,但实际的极限远不及此。由于. net程序中的单个对象不可能超过2GB,并且字符串类型使用UTF-16(每个字符2字节),所以您最多可以做到1,073,741,823,但是您不太可能在32位计算机上分配它。
这是一种“如果你必须问,你可能做错了什么”的情况。
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
对于那些晚到这个话题的人来说,我可以看到hitscan的“你可能不应该这样做”可能会让一些人问他们应该怎么做……
StringBuilder类通常是一个简单的替代。 如果您的数据来自一个文件,请特别考虑一个基于流的类。
s += "stuff"的问题是,它必须分配一个全新的区域来保存数据,然后将所有旧数据复制到它加上新数据-每一次循环迭代。因此,用s += "stuff"在1,000,000中增加5个字节的开销非常大。 如果你想只写5个字节到最后,然后继续你的程序,你必须选择一个留出一些增长空间的类:
StringBuilder sb = new StringBuilder(5000);
for (; ; )
{
sb.Append("stuff");
}
当StringBuilder达到限制时,它会自动增长一倍。所以,你会在开始时看到增长的痛苦,一次是5000字节,一次是10000字节,一次是20000字节。追加字符串将在每次循环迭代中引起痛苦。
在我的机器上,字符串的最大长度是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。
如果大家能在下面留言,我会非常感激的。
如果人们得到相同或不同的结果,这将是很有趣的。