简体   繁体   English

StringBuilder由链表备份

[英]StringBuilder backed up by a linked list

I notice that StringBuilder is backed up by an array, which makes it allocate a single memory block. 我注意到StringBuilder由一个数组备份,这使得它分配一个内存块。 I don't like that behavior because the memory is highly fragmented and somtimes it's hard to find a big enough block of memory, which triggers the GC... 我不喜欢这种行为,因为内存是高度分散的,有时很难找到足够大的内存块,这会触发GC ......

I wonder if there is a version of StringBuilder backed up by a linked list, which would be more memory-friendly. 我想知道是否有链接列表备份的StringBuilder版本,这将更加内存友好。

First of all I'd seriously question the need for an alternative: for most use-cases the implementation of StringBuilder is perfectly sensible and works just fine. 首先,我认真地质疑是否需要替代方案:对于大多数用例, StringBuilder的实现是完全合理的,并且工作得很好。 Measure before you decide. 在你决定之前测量。

If you indeed need similar objects of such enormous size that this becomes a problem, then something like Ropes for Java might help: It's effectively a library of many different CharacterSequence implementations (with a common interface Rope ) optimized for different interactions. 如果你确实需要这么大的类似对象,这就成了一个问题,那么像Ropes for Java这样的东西可能有所帮助:它实际上是一个针对不同交互进行了优化的许多不同CharacterSequence实现(具有通用接口Rope )的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM