简体   繁体   English

Java Char Buffer自动增长并可以重复使用

[英]Java Char Buffer that automatically grows and can be re-used

Hello Java Developers: 您好Java开发人员:

I'm looking for a built in class to store a sequence of characters. 我正在寻找一个内置的类来存储字符序列。 It needs to meet the following criteria: 它需要满足以下条件:

  1. It can store char values (not just byte values) 它可以存储char值(不仅仅是字节值)
  2. It has an append method that causes the underlying data structure to automatically grow in size should it exceed the pre-allocated size. 它具有一个append方法,如果基础数据结构超过预分配的大小,该方法将使基础数据结构的大小自动增长。
  3. Has a method to clear the contents (ie re-use the memory). 有清除内容的方法(即重新使用内存)。
  4. Can read the entry at a given index. 可以读取给定索引的条目。

I've found several options that meet some of these requirements but not all of them. 我找到了一些满足其中一些要求的选项,但并非全部。 For instance: 例如:

  1. CharBuffer does not satisfy 2. CharBuffer不满足2。
  2. StringBuffer does not satisfy 3. StringBuffer不满足3。
  3. ByteArrayOutputStream does not satisfy 1. ByteArrayOutputStream不满足1。
  4. CharArrayWriter does not satisfy 4. CharArrayWriter不满足4。

Am I misinterpreting the documentation for one of these classes, or does someone out there know of a data structure that meets all 4 criteria? 我是否误解了其中一个类的文档,还是那里的某个人知道满足所有这四个条件的数据结构?

Edit: By criteria 3, I don't mean free memory to GC. 编辑:按条件3,我并不是说要释放GC的内存。

I'm in the situation where I need to repeatedly allocate and de-allocate several of these buffers. 我处于需要重复分配和取消分配其中一些缓冲区的情况。 In my use case, these buffers end up being fairly large (~ 100 KB) and I would have several threads using such buffers. 在我的用例中,这些缓冲区最终会变得很大(〜100 KB),我将有多个线程使用此类缓冲区。

I'm aware that I could just create a buffer, use it and then have it GC-ed when I'm done. 我知道我可以创建一个缓冲区,使用它,然后在完成后进行GC处理。 However, I've found that the JVM garbage collector runs quite slowly when you are constantly allocating and de-allocating a large percent of the JVM (and physical machine's) memory. 但是,我发现当您不断分配和取消分配大部分JVM(和物理机)内存时,JVM垃圾收集器的运行速度非常慢。 I've found speed improvements when I start micromanaging the memory (like one can do in C++) by allocating a buffer once and then re-using it (thus triggering garbage collection less frequently). 当我开始微管理内存(就像在C ++中可以做到的那样)时,我发现速度得到了提高,方法是分配一次缓冲区,然后重新使用它(因此触发垃圾回收的频率降低)。

StringBuilder/StringBuffer should satisfy 3 fine since delete(...) should do what you need. StringBuilder / StringBuffer应该满足3的要求,因为delete(...)应该可以满足您的要求。 Or why not simply create a new instance and let the old one be GC'd? 还是为什么不简单地创建一个新实例并让旧实例成为GC?

StringBuffer可以删除其内容

buffer.delete(0, buffer.length());

Solution that I've found: StringBuilder has .setLength(0) which I can use to get the buffer to start re-writing contents. 我发现的解决方案:StringBuilder具有.setLength(0),可用来获取缓冲区以开始重写内容。

(A dedicated .reset() method would have been nice.) (专用的.reset()方法会很好。)

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

相关问题 Spring Boot:如何重用@EnableAutoConfiguration? - Spring Boot: how can @EnableAutoConfiguration be re-used? Mono的/ Flux的可以重用/它们是不变的+无状态的吗? - Can Mono's / Flux's be re-used / are they immutable + stateless? 哈希图被新对象重用 - hashmap being re-used by new objects Hadoop 和重复使用的可变可写字段 - Hadoop and re-used mutable writable fields 是否可以将java.sql和javax.sql中的某些接口重新用于NoSQL? - Could some interfaces in java.sql and javax.sql be re-used for NoSQL? 如何创建可被多个Android项目重用的类的jar文件? - How do you create a jar file of classes that can be re-used by multiple Android projects? 基础 gradle 项目维护公共依赖关系可以在所有其他微服务项目中重复使用 - Base gradle project to maintain common dependencies can be re-used across all other micro service projects 调用invalidate后重新使用会话ID - Session ID re-used after call to invalidate Java - Char缓冲区问题 - Java - Char Buffer Issue Android:设置自定义列表项的可能/智能方法? (或者方便的方法来设置重复使用的布局) - Android: Possible/Smart way to setup custom list items? (Or convenient way to setup re-used layouts)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM