Chronicle Queue Read/write from queue using zero allocation

I am a new user of chronicle queue and I wanna use a zero allocation strategy to read and write the objects from chronicle queue. I want to use a queue and a marshable implementation of bytes like a pojo class is the that some correct strategy? I did not found anything to help me. I try to do a instance of message class below for append and read the content from the queue. When i try to read with more than one thread I always get out of memory error.

public class Message implements BytesMarshallable{
    private byte[] text;
    private long timeStamp;
    
    public Message(){}

    //Getters and Setters
}

I tryed to read using tailer.readDocument when i got out of memory error with more than one thread

Upvotes: 2

Views: 1507

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533482

Using byte[] requires you to allocate a new one every time the size changes. You can't change the size of a byte[].

Instead, I suggest using out Bytes class which can be resized without creating much garbage (only sometimes when it grows)

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;
    }
}

See this example https://github.com/OpenHFT/Chronicle-Queue-Demo/tree/master/messages-with-text

When run with a small heap -Xmx128m -XX:NewSize=96m -verbose:gc MessageMain you can see that millions of messages trigger no collections.

Read 10,000,000 of 10,000,000 messages in 7.990 seconds
Read 10,000,000 of 10,000,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,000 of 10,000,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,000 of 10,000,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,000 of 10,000,000 messages in 7.013 seconds
Read 10,000,000 of 10,000,000 messages in 6.838 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-465974753213/metadata.cq4t

Upvotes: 2

Related Questions