最有效的StringBuilder初始容量大小? [英] Most efficient initial capacity size for StringBuilder?

查看:666
本文介绍了最有效的StringBuilder初始容量大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写很多东西来登录突发,并优化数据路径.我使用StringBuilder构建日志文本.从内存管理角度来看,最有效的初始容量是什么,因此不管使用JVM,它都能正常工作吗?目标是几乎总是避免重新分配,这应该由大约80-100的初始容量覆盖.但是我也想浪费尽可能少的字节,因为StringBuilder实例可能会在缓冲区中徘徊,浪费的字节会大量出现.

I'm writing lots of stuff to log in bursts, and optimizing the data path. I build the log text with StringBuilder. What would be the most efficient initial capacity, memory management wise, so it would work well regardless of JVM? Goal is to avoid reallocation almost always, which should be covered by initial capacity of around 80-100. But I also want to waste as few bytes as possible, since the StringBuilder instance may hang around in buffer and wasted bytes crop up.

我意识到这取决于JVM,但是应该有一些值,这将浪费最少的字节数,而与JVM无关,这是最小公分母".我目前正在使用128-16,其中128是一个不错的整数,而减法是分配开销.另外,这可能被认为是过早优化"的一种情况,但是由于我要回答的是一个经验法则"数字,因此知道将来也会有用.

I realize this depends on JVM, but there should be some value, which would waste least bytes, no matter the JVM, sort of "least common denominator". I am currently using 128-16, where the 128 is a nice round number, and subtraction is for allocation overhead. Also, this might be considered a case of "premature optimization", but since the answer I am after is a "rule-of-a-thumb" number, knowing it would be useful in future too.

我不期望我的最佳猜测"答案(上面我自己的答案已经是),我希望有人已经对此进行了研究并且可以共享基于知识的答案.

I'm not expecting "my best guess" answers (my own answer above is already that), I hope someone has researched this already and can share a knowledge-based answer.

推荐答案

好吧,我最终自己做了简短的测试,然后在评论后进行了更多测试,以得到经过编辑的答案.

Well, I ended up testing this briefly myself, and then testing some more after comments, to get this edited answer.

使用JDK 1.7.0_07并测试报告VM名称为"Java HotSpot(TM)64位服务器VM"的应用程序,StringBuilder内存使用情况的粒度为4个字符,甚至增加了4个字符

Using JDK 1.7.0_07 and test app reporting VM name "Java HotSpot(TM) 64-Bit Server VM", granularity of StringBuilder memory usage is 4 chars, increasing at even 4 chars.

答案: 4的任意倍数从内存分配的角度来看,至少对于此64位JVM来说,对于StringBuilder来说是同样好的容量.

Answer: any multiple of 4 is equally good capacity for StringBuilder from memory allocation point of view, at least on this 64-bit JVM.

通过在不同的测试程序执行中(具有相同的初始堆状态)创建1000000个具有不同初始容量的StringBuilder对象进行测试,并在打印前后打印ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed().

Tested by creating 1000000 StringBuilder objects with different initial capacities, in different test program executions (to have same initial heap state), and printing out ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() before and after.

还可以打印出堆大小,这也证实了,从堆实际为每个StringBuilder缓冲区分配的数量是8字节的偶数倍,这与Java字符长2个字节一样,这是我们所期望的.换句话说,分配1000000个初始容量为1..4的实例所需的内存要比分配相同数量的初始容量为5 ... 8的等项少8兆字节(每个实例8个字节).

Printing out heap sizes also confirmed, that amount actually allocated from heap for each StringBuilder's buffer is an even multiple of 8 bytes, as expected since Java char is 2 bytes long. In other words, allocating 1000000 instances with initial capacity 1..4 takes about 8 megabytes less memory (8 bytes per instace), than allocating same number of isntances with initial capacity 5...8.

这篇关于最有效的StringBuilder初始容量大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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