MappedByteBuffer读写文件

一. MappedByteBuffer

  1. java把文件映射到内存中,避免堆内存产生大对象引起full gc。mappedByteBuffer的读写速度都要超过堆内读写文件的速度

    public class AA {
        public static void main(String[] args) throws IOException, InterruptedException {
            map();
            //fis();
        }
    
        private static void fis() throws IOException {
            /*long l = System.currentTimeMillis();
            FileOutputStream fos = new FileOutputStream(new File("d://1234.txt"));
            for(int i=0;i<4000000;i++)
                fos.write('a');
            System.out.println((System.currentTimeMillis()-l));   //13011*/
    
            long l = System.currentTimeMillis();
            FileInputStream fis = new FileInputStream(new File("d://1234.txt"));
            int read;
            for(int i=0;i<4000000;i++) {
                read = fis.read();
            }
            System.out.println((System.currentTimeMillis()-l));  //9774
        }
    
        private static void map() throws IOException {
            /*long l = System.currentTimeMillis();
            FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "rw").getChannel();
            CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4000000*2).asCharBuffer();
            System.out.println("start");
            for(int i=0;i<4000000;i++)
                mappedByteBuffer.put('a');
            System.out.println((System.currentTimeMillis()-l));
            fileChannel.close(); //47*/
    
            long l = System.currentTimeMillis();
            FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "r").getChannel();
            CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()).asCharBuffer();
            System.out.println("start");
            for(int i=0;i<4000000;i++)
                mappedByteBuffer.get();
            System.out.println((System.currentTimeMillis()-l));
            fileChannel.close();  //40
        }
    }
    
    
  2. 关闭MappedByteBuffer

    public final class IOUtil {
        private static final Logger LOGGER = LoggerFactory.getLogger(IOUtil.class);
    
        private IOUtil() {
        }
    
        public static void closeMappedByteBuffer(final MappedByteBuffer byteBuffer) {
            if (byteBuffer == null) {
                return;
            }
    
            if (byteBuffer != null) {
                AccessController.doPrivileged(
                        new PrivilegedAction<Object>() {
                            @Override
                            public Object run() {
                                try {
                                    Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner", new Class[0]);
                                    cleanerMethod.setAccessible(true);
                                    sun.misc.Cleaner cleaner = (sun.misc.Cleaner) cleanerMethod.invoke(byteBuffer,
                                            new Object[0]);
                                    cleaner.clean();
                                } catch (NoSuchMethodException e) {
                                    LOGGER.error("error occurred when clean mapped byte buffer", e);
                                } catch (InvocationTargetException e) {
                                    LOGGER.error("error occurred when clean mapped byte buffer", e);
                                } catch (IllegalAccessException e) {
                                    LOGGER.error("error occurred when clean mapped byte buffer", e);
                                }
                                return null;
                            }
                        }
                );
            }
        }
    }
    
    
原文地址:https://www.cnblogs.com/72808ljup/p/5422301.html