MappedByteBuffer强制释放后读取引发jvm crash
?
部分信息如下:
#
# A fatal error has beendetected by the Java Runtime Environment:
#
#? EXCEPTION_ACCESS_VIOLATION (0xc0000005) atpc=0x01b8d4d7, pid=10236, tid=8452
#
# JRE version: 6.0_18-b02
# Java VM: Java HotSpot(TM)Client VM (16.0-b09 mixed mode, sharing windows-x86 )
# Problematic frame:
# J? java.nio.DirectByteBuffer.get(I)B
#
# An error report file withmore information is saved as:
#D:\workspace_new\metamorphosis\metamorphosis-server\hs_err_pid10236.log
#
# If you would like to submit abug report, please visit:
#??http://java.sun.com/webapps/bugreport/crash.jsp
@Test
public void unmapAndRead()throws Exception {
? File file =new File("unmapAndRead.txt");
? FileChannel fileChannel =new RandomAccessFile(file,"rw").getChannel();
? MappedByteBuffer buf = fileChannel.map(MapMode.READ_WRITE, 0, 4096);
? buf.putInt(5);
? buf.force();
? this.unmap(buf);
? buf.get(2);//Crash occur
}
?
void unmap(finalMappedByteBuffer mappedByteBuffer) {
? try {
? ? if (mappedByteBuffer ==null) {
? ? return;
? }
? mappedByteBuffer.force();
? AccessController.doPrivileged(new PrivilegedAction<Object>() {
? @Override
? public Object run() {
? ? try {
? ? ? ?Method getCleanerMethod = ? ? ?mappedByteBuffer.getClass().getMethod("cleaner",new Class[0]);
? ? ? ?getCleanerMethod.setAccessible(true);
? ? ? ?sun.misc.Cleaner cleaner =
? ? ? ?(sun.misc.Cleaner)getCleanerMethod.invoke(mappedByteBuffer,new?Object[0]);
? ? ? ?cleaner.clean();
? ? }
? ? catch (Exception e) {
? ? ? ?e.printStackTrace();
? ? }
? ? return null;
? ?}
? });
?
?}
catch (Exception e) {
? ?e.printStackTrace();
}
}
?