Chronicle Bytes Release Method Deprecated

267 views Asked by At

I recently moved to JDK 17 from JDK 11 which triggered an update to the Chronicle Wire library. We used to use version 2.17.50 with JDK 11 and now we have moved to chronicle-wire version 2.22.17 and chronicle-bytes version 2.22.21.

Following code used to work fine with JDK 11 but not with JDK 17. It seems the release method is deprecated.

public static Object deserializeFromBinary(final byte[] rawBytes, final Class<?> classVal, final String bookmark) 
{
   Bytes<?> bytesDeserialize = Bytes.allocateDirect(rawBytes);
   Wire wire = new BinaryWire(bytesDeserialize);
   try {
       return wire.read(() -> bookmark).object(classVal);
   } finally {
       bytesDeserialize.release();
   }
}

How can we release native bytes now? Anyone has come across the same situation?

1

There are 1 answers

0
Peter Lawrey On

This resource, in its simplest form, has a reference counter. In more complex scenarios, it tracks owners of the Bytes to ensure the same owner doesn't reserve or release twice.

As you don't need owners, you can use releaseLast() how even simpler than that you don't need to allocate direct memory.

In this example, the Bytes use an on-heap byte[] so don't need to be released.

public static Object deserializeFromBinary(final byte[] rawBytes, final Class<?> classVal, final String bookmark) {
    return WireType.BINARY_LIGHT.apply(Bytes.wrapForRead(rawBytes))
            .read(bookmark)
            .object(classVal);
}

The WireType.BINARY_LIGHT turns off some expensive options you probably don't need.

Ideally, you shouldn't be creating byte[] or new Wire on each object if this is called often.