In Java the memory allocated by NIO direct buffers is freed with sun.misc.Cleaner
instances, some special phantom references that are more efficient than object fin
Hope it help you if using java9.
The code below was already tested in Intellij IDEA 2017 and oracle jdk 9.
import java.lang.ref.Cleaner;
public class Main {
public Main() {
}
public static void main(String[] args) {
System.out.println("Hello World!");
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Cleaner cleaner = Cleaner.create();
Main obj = new Main();
cleaner.register(obj, new Runnable() {
@Override
public void run() {
System.out.println("Hello World!222");
}
});
System.gc();
}
}
}