什么是StringBuilder的的内存消耗是怎样的? [英] What is StringBuilder's RAM consumption like?

查看:206
本文介绍了什么是StringBuilder的的内存消耗是怎样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个操作,我们正在做大量的大型字符串连接,并于最近遇到了内存溢出异常。不幸的是,调试code不是一种选择,因为这是发生在客户站点。

We have a few operations where we are doing a large number of large string concatenations, and have recently encountered an out of memory exception. Unfortunately, debugging the code is not an option, as this is occurring at a customer site.

因此​​,寻找到我们的code进行大修之前,我想问:是什么的StringBuilder的RAM消费特点为大字符串?

So, before looking into a overhaul of our code, I would like to ask: what is the RAM consumption characteristics of StringBuilder for large strings?

特别是因为他们比较标准的字符串类型。字符串的大小都超过10兆,而我们似乎碰到问题,大约20 MB。

Especially as they compare to the standard string type. The size of the strings are well over 10 MB, and we seem to run into the issues around 20 MB.

注意:这不是关于速度,RAM

NOTE: This is not about speed but RAM.

推荐答案

下面是关于的字符串连接VS内存分配

如果你能避免串联,做到这一点!

这是没有道理的,如果你不   有来连接,但想你的   来源$ C ​​$ C到好看,使用   第一种方法。它会得到优化为   如果它是一个字符串。

This is a no brainer, if you don't have to concatenate but want your source code to look nice, use the first method. It will get optimized as if it was a single string.

不要使用+ =串联如初。太多的变化正在发生   的场景,这是不明显落后   从我的code摆在首位。一世   奉劝还是用String.Concat()   明确地与任何过载(2   串,3串,字符串数组)。   这将清楚地表明你的code   确实没有任何惊喜,而   让自己保持检查   效率

Don't use += concatenating ever. Too much changes are taking place behind the scene, which aren't obvious from my code in the first place. I advise to rather use String.Concat() explicitly with any overload (2 strings, 3 strings, string array). This will clearly show what your code does without any surprises, while allowing yourself to keep a check on the efficiency.

试着估计一个StringBuilder的目标大小。

更精确,你可以估算出   需要的尺寸,更低的临时   串StringBuilder的将要   建立以提高其内部   缓冲区。

The more accurate you can estimate the needed size, the less temporary strings the StringBuilder will have to create to increase its internal buffer.

不要使用任何格式()方法时,性能是一个问题。

过多的开销是参与   解析格式时,你可以   构建一个数组出片的时候   你正在使用的是{X}取代。   格式()是良好的可读性,但   的事情之一,当你去   压榨所有可能的表现出来   您的应用程序。

Too much overhead is involved in parsing the format, when you could construct an array out of pieces when all you are using are {x} replaces. Format() is good for readability, but one of the things to go when you are squeezing all possible performance out of your application.

这篇关于什么是StringBuilder的的内存消耗是怎样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆