.NET 字符串的最大可能长度是多少?
.NET 中可以创建的最长字符串是多少? 据我所知,String
类的文档对这个问题保持沉默,因此权威的答案可能需要一些内部知识。 64 位系统上的最大值会发生变化吗?
[这更多的是出于好奇而不是实际用途 - 我不打算创建任何使用巨大字符串的代码!]
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!]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
理论限制可能是 2,147,483,647,但实际限制远非如此。 由于 .NET 程序中的单个对象可能不会超过 2GB,并且字符串类型使用 UTF-16(每个字符 2 个字节),因此您可以做的最好的事情是 1,073,741,823,但您不太可能能够分配它在 32 位机器上。
这是其中一种情况,“如果您必须问,您可能是做错事了。”
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 UTF-16 (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."
根据我高度科学且准确的实验,它在我的机器上早在 1,000,000,000 个字符之前就达到了顶峰。 (我仍在运行下面的代码以获得更好的精确定位)。
更新:
几个小时后,我放弃了。 最终结果:可以超过 100,000,000 个字符,在 1,000,000,000 个字符时立即给出 System.OutOfMemoryException。
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 code below 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.由于
System.String
的Length
属性是Int32
,因此我猜测最大长度为 2,147,483,647 个字符(最大Int32 大小)。 如果允许更长的时间,您将无法检查长度,因为那会失败。
Since the
Length
property ofSystem.String
is anInt32
, I would guess that that the maximum length would be 2,147,483,647 chars (maxInt32
size). If it allowed longer you couldn't check the Length since that would fail.对于迟到的人来说,我可以看到 hitscan 的“你可能不应该这样做”可能会导致有人问他们应该做什么......
StringBuilder 类通常很容易替换。
考虑基于流的类之一特别是,如果您的数据来自文件。
s += "stuff"
的问题在于它必须分配一个全新的区域来保存数据,然后将所有旧数据以及新数据复制到其中 - 每个循环迭代。 因此,使用s += "stuff"
将 5 个字节添加到 1,000,000 的成本极其高昂。如果您想要的只是在末尾写入五个字节并继续执行您的程序,则必须选择一个留有一些增长空间的类:
StringBuilder
将达到限制时加倍自动增长。 因此,您会在开始时看到一次增长的痛苦,一次在 5,000 字节,一次在 10,000 字节,再次在 20,000 字节。 每次循环迭代时附加字符串都会带来痛苦。For anyone coming to this topic late, I could see that hitscan's "you probably shouldn't do that" might cause someone to ask what they should do…
The StringBuilder class is often an easy replacement.
Consider one of the stream-based classes especially, if your data is coming from a file.
The problem with
s += "stuff"
is that it has to allocate a completely new area to hold the data and then copy all of the old data to it plus the new stuff - EACH AND EVERY LOOP ITERATION. So, adding five bytes to 1,000,000 withs += "stuff"
is extremely costly.If what you want is to just write five bytes to the end and proceed with your program, you have to pick a class that leaves some room for growth:
StringBuilder
will auto-grow by doubling when it's limit is hit. So, you will see the growth pain once at start, once at 5,000 bytes, again at 10,000, again at 20,000. Appending strings will incur the pain every loop iteration.我的机器上字符串的最大长度是1,073,741,791。
您会看到,字符串并不像人们普遍认为的那样受到整数的限制。
除了内存限制之外,字符串的长度不能超过 230 (1,073,741,824) 个字符,因为 Microsoft CLR(公共语言运行时)规定了 2GB 的限制。 超过我的计算机允许的 33 个。
现在,欢迎您亲自尝试一下。
在 Visual Studio 中创建一个新的 C# 控制台应用程序,然后将 main 方法复制/粘贴到此处:
我的结果如下:
我的机器上字符串的最大长度是 1073741791。
如果人们可以将他们的结果作为评论发布在下面,我将非常感激。
了解人们是否得到相同或不同的结果将会很有趣。
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:
My results were as follows:
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.
由于
String.Length
是一个整数(即Int32
的别名),因此其大小仅限于Int32.MaxValue
unicode 字符。 ;-)Since
String.Length
is an integer (that is an alias forInt32
), its size is limited toInt32.MaxValue
unicode characters. ;-)字符串在 RAM 堆中分配动态内存大小。 但字符串地址存储在占用4字节内存的堆栈中。
String allocates dynamic memory size in the heap of your RAM. But string address is stored in stack that occupies 4 bytes of memory.
200 兆...此时您的应用程序几乎停止运行,大约有千兆工作集内存,并且操作系统开始表现得好像您需要重新启动一样。
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.