zoukankan      html  css  js  c++  java
  • 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;
                              }
                          }
                  );
              }
          }
      }
      
      
  • 相关阅读:
    yii2中的url美化
    一级域名301重定向到www二级域名
    使用meta来控制浏览器的渲染方式
    同一页面不同编码的提交处理
    Yii2.0 UrlManager
    sqlsever连接两个不同服务器上的数据库进行查询
    php 实现传入参数的传出
    xcode如何修改项目名称
    ios如何实现应用之间的跳转
    ios程序如何实现系统自带的分享
  • 原文地址:https://www.cnblogs.com/72808ljup/p/5422301.html
Copyright © 2011-2022 走看看