问题描述
我是编年史队列的新用户,我想使用零分配策略从编年史队列中读取和写入对象。我想使用一个队列和一个像 pojo 类这样的字节的 marshable 实现是一些正确的策略吗? 我没有找到任何可以帮助我的东西。 我尝试在下面做一个消息类的实例,用于追加和读取队列中的内容。当我尝试使用多个线程读取时,总是出现内存不足错误。
public class Message implements BytesMarshallable{
private byte[] text;
private long timeStamp;
public Message(){}
//Getters and Setters
}
当我遇到多个线程的内存不足错误时,我尝试使用 tailer.readDocument 读取
解决方法
使用 byte[]
要求您在每次大小更改时分配一个新的。您无法更改 byte[]
的大小。
相反,我建议使用 Bytes
类,它可以在不产生太多垃圾的情况下调整大小(仅当它增长时)
package run.chronicle.queue;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.wire.BytesInBinaryMarshallable;
import net.openhft.chronicle.wire.LongConversion;
import net.openhft.chronicle.wire.MilliTimestampLongConverter;
public class Message extends BytesInBinaryMarshallable {
private final Bytes text = Bytes.allocateElasticOnHeap();
@LongConversion(MilliTimestampLongConverter.class)
private long timeStamp;
//Getters and Setters
public Bytes getText() {
return text;
}
public void setText(CharSequence text) {
this.text.clear().append(text);
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}
看这个例子https://github.com/OpenHFT/Chronicle-Queue-Demo/tree/master/messages-with-text
当使用小堆 -Xmx128m -XX:NewSize=96m -verbose:gc MessageMain
运行时,您可以看到数百万条消息不会触发任何集合。
Read 10,000,000 of 10,000 messages in 7.990 seconds
Read 10,000 messages in 6.907 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-438402668456/metadata.cq4t
Read 10,000 messages in 6.836 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-445239126125/metadata.cq4t
Read 10,000 messages in 6.883 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 3 ms to add mapping for test-452122895277/metadata.cq4t
Read 10,000 messages in 7.013 seconds
Read 10,000 messages in 6.838 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-465974753213/metadata.cq4t