Etienne
Etienne

Reputation: 11

How to force deallocation memory mapping of a MappedByteBuffer in Java 11 - java.lang.ClassNotFoundException: sun.misc.Cleaner

I'm currently migrating the application code from java 8 to java 11, previously we use the internal class "sun.misc.Cleaner" and now when my application run I got a java.lang.ClassNotFoundException: sun.misc.Cleaner.

When i google it i found that i should use java.lang.ref.Cleaner to do it the same way, but i want to be sure that this is the right approach as i read that it will necessite the activation of internal module in my jvm.

Could you help me please ?

* Clean a <code>MappedByteBuffer</code>. Method to call to delete memory of the file and free the resources
* @param pByteBuffer Buffer to clean
*/
private void freeByteBuffer(ByteBuffer pByteBuffer) {
    try {
          Method lCleanerMethod = pByteBuffer.getClass().getMethod("cleaner");
          lCleanerMethod.setAccessible(true);
          Method lCleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");
          lCleanMethod.setAccessible(true);
          lCleanMethod.invoke(lCleanerMethod.invoke(pByteBuffer));
         } catch (Exception lEx) {
           logger.error(KernelError.FREE_MEM, idefixComponentName, lEx); 
        }
    }

I expected the code to work the same way as it is in the spec of the software

Upvotes: 0

Views: 48

Answers (0)

Related Questions