Memory mapping is used in Java to upload instances of large files

  • 2020-04-01 03:36:50
  • OfStack

When dealing with large files, if ordinary FileInputStream, FileOutputStream or RandomAccessFile are used for frequent read and write operations, the process will be slowed down due to frequent read and write to external storage. The following is a comparison experiment.


package test;  import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel;  public class Test {      public static void main(String[] args) { 
        try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=fis.read())>=0){ 
                    sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }          try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            BufferedInputStream bis=new BufferedInputStream(fis); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=bis.read())>=0){ 
                    sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }          MappedByteBuffer buffer=null; 
        try { 
            buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            for(int i=0;i<1253244;i++){ 
                n=0x000000ff&buffer.get(i); 
                sum+=n; 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }      }  }

The test file is a file of size 1253,244 bytes. Test results:

sum:220152087 time:1464 
sum:220152087 time:72 
sum:220152087 time:25

The read data is correct. Delete the data processing part.

package test;  import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel;  public class Test {      public static void main(String[] args) { 
        try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=fis.read())>=0){ 
                    //sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }          try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            BufferedInputStream bis=new BufferedInputStream(fis); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=bis.read())>=0){ 
                    //sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }          MappedByteBuffer buffer=null; 
        try { 
            buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            for(int i=0;i<1253244;i++){ 
                //n=0x000000ff&buffer.get(i); 
                //sum+=n; 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }      }  }

Test results:

sum:0 time:1458 
sum:0 time:67 
sum:0 time:8

Thus, it is much faster to map some or all of a file to memory for reading and writing.

This is because the memory mapping file will first peripheral storage file on mapped to a continuous area in memory, as a byte array processing, read and write operations directly on the memory, then will memory area to map to peripheral storage file, this has saved frequent among foreign save time for reading and writing, greatly reduces the time to read and write.


Related articles: