java mmap介绍

java mmap介绍

1.优点

与常规I / O相比,内存映射IO具有以下优点:

  • 用户进程将文件数据视为内存,因此无需发出read()或write()系统调用。
  • 当用户进程触摸映射的内存空间时,将自动生成页错误,以从磁盘引入文件数据。如果用户修改了映射的内存空间,则受影响的页面会自动标记为脏页面,随后将刷新到磁盘以更新文件。
  • 操作系统的虚拟内存子系统将执行页面的智能缓存,并根据系统负载自动管理内存。
  • 数据始终是页面对齐的,不需要复制缓冲区。
  • 可以映射非常大的文件,而无需消耗大量内存来复制数据。

2.示例代码----写

    public static void readTest(){

        String  bigFile = "D:/newfile.txt";


        //Get file channel in read-only mode
        try(RandomAccessFile file = new RandomAccessFile(new File(bigFile),"r")){

            FileChannel fileChannel = file.getChannel();
            
            //Get direct byte buffer access using channel.map() operation
            MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileChannel.size());

            // the buffer now reads the file as if it were loaded in memory.
            System.out.println(buffer.isLoaded());  //prints false
            System.out.println(buffer.capacity());  //Get the size based on content size of file
            
            //You can read the file from this buffer the way you like.
            for (int i = 0; i < buffer.limit(); i++) {
                System.out.println((char)buffer.get());
            }

        }catch(FileNotFoundException f){
            f.printStackTrace();
        }catch(IOException o){
            o.printStackTrace();
        }

    }

3.代码示例---写

public static void writeTest(){

        String  bigFile = "D:/newfile.txt";

        String  content = "java.com";

        File file = new File(bigFile);

        file.delete();

        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw")){

            FileChannel fileChannel = randomAccessFile.getChannel();

            // Get direct byte buffer access using channel.map() operation
            MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.length()+10);

            //Write the content using put methods
            buffer.put("java.com".getBytes());

        }catch(FileNotFoundException f){
            f.printStackTrace();
        }catch(IOException o){
            o.printStackTrace();
        }



    }

4.参考资料

https://howtodoinjava.com/java/nio/memory-mapped-files-mappedbytebuffer/

https://xunnanxu.github.io/2016/09/10/It-s-all-about-buffers-zero-copy-mmap-and-Java-NIO/

 

posted on 2021-11-18 21:17  真情的风  阅读(651)  评论(0)    收藏  举报