#Proper way to clean a DirectByteBuffer in JDK 11+

9 messages · Page 1 of 1 (latest)

peak mist
#

What's the proper way to clean a DirectByteBuffer? In Java 8 you could do

void clean(DirectByteBuffer dbb) {
  Cleaner cleaner = dbb.cleaner();
  if (cleaner != null) {
    cleaner.clean();
  }
}

Currently I'm using sun.misc.Unsafe

void clean(DirectByteBuffer dbb) {
  Unsafe unsafe = myGetUnsafe();
  unsafe.invokeCleaner(dbb);
}

But I don't believe that's the correct way to go about it. I know java.lang.ref.Cleaner exists, but I couldn't get it working with this. I had something like

void clean(DirectByteBuffer dbb) {
  Cleaner cleaner = Cleaner.create();
  cleaner.register(dbb, new Runnable() {
    public void run() {
      Cleaner cleaner = dbb.cleaner();
      if (cleaner != null) {
        cleaner.clean();
      }
    }
  });
}

I thought the later would work because the Runnable is invoked by internal JDK stuff which would mean that since it's within the same module, this would be allowed, but I'm getting IllegalAccessException here.

So what's the correct way to clean a DirectByteBuffer object in JDK 11+?

velvet hazelBOT
#

This post has been reserved for your question.

Hey @peak mist! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

lament shard
#

doesnt a DirectByteBuffer registers its cleaner itself? or isnt this the java one?

peak mist
lament shard
#

ah

neon sable
#

Have you tried the usual --add-export solutions?

#

Though technically, if it was a good idea to do that then Java would make it easy

peak mist
#

Yes, but that's not really an option at the moment