[英]Chronicle Queue memory mapping file to reduce avoid garbage collection?
我對Chronicle隊列如何避免垃圾收集有疑問:
我知道Chronicle隊列使用內存映射文件,以便可以將對象保存到主內存或dist中,而不是保存到JVM中。 但是,當處理器從主存儲器反序列化對象時,它仍需要創建一個新實例。 那么編年史隊列到底在哪里避免垃圾收集呢?
請參閱以下來自Chronicle github示例的案例。 當執行寫/讀操作時,它仍然需要使用MyObject創建新實例me = new MyObject(),並且將“ me”進行垃圾回收。
public class Example {
static class MyObject implements Marshallable {
String name;
int age;
@Override
public String toString() {
return Marshallable.$toString(this);
}
}
public static void main(String[] args) throws IOException {
// will write the .cq4 file to working directory
SingleChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(Files
.createTempDirectory("queue").toFile()).build();
ExcerptAppender appender = queue.acquireAppender();
ExcerptTailer tailer = queue.createTailer();
MyObject me = new MyObject();
me.name = "rob";
me.age = 40;
// write 'MyObject' to the queue
appender.writeDocument(me);
// read 'MyObject' from the queue
MyObject result = new MyObject();
tailer.readDocument(result);
System.out.println(result);
}
}
您可以重用反序列化的對象。
// created once
MyObject result = new MyObject();
// this can be called multiple times with the same object
tailer.readDocument(result);
String
也被合並以減少垃圾。
這樣,您可以編寫和讀取數百萬條消息,但只能創建一個或兩個對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.