Java uses memory mapping for large file uploads

Source: Internet
Author: User

When dealing with large files, frequent read and write operations using ordinary FileInputStream or FileOutputStream or randomaccessfile will cause the process to slow down due to frequent read and write external memory. The following is a comparison experiment.

 Packagetest; ImportJava.io.BufferedInputStream; ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; Importjava.io.IOException; ImportJava.io.RandomAccessFile; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel;  Public classTest { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); intSum=0; intN; Longt1=System.currenttimemillis (); Try {                   while((N=fis.read ()) >=0) {sum+=N; }              } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }          Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intSum=0; intN; Longt1=System.currenttimemillis (); Try {                   while((N=bis.read ()) >=0) {sum+=N; }              } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Mappedbytebuffer Buffer=NULL; Try{buffer=NewRandomaccessfile ("/home/tobacco/test/res.txt", "RW"). Getchannel (). Map (FileChannel.MapMode.READ_WRITE, 0, 1253244); intSum=0; intN; Longt1=System.currenttimemillis ();  for(inti=0;i<1253244;i++) {n=0x000000ff&Buffer.get (i); Sum+=N; }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }      }  }

The test file is a file with a size of 1253244 bytes. Test results:

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

Indicates that the read data is correct. Delete the data processing part thereof.

ImportJava.io.BufferedInputStream; ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; Importjava.io.IOException; ImportJava.io.RandomAccessFile; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel;  Public classTest { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); intSum=0; intN; Longt1=System.currenttimemillis (); Try {                   while((N=fis.read ()) >=0){                      //sum+=n;                 }              } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }          Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intSum=0; intN; Longt1=System.currenttimemillis (); Try {                   while((N=bis.read ()) >=0){                      //sum+=n;                 }              } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Mappedbytebuffer Buffer=NULL; Try{buffer=NewRandomaccessfile ("/home/tobacco/test/res.txt", "RW"). Getchannel (). Map (FileChannel.MapMode.READ_WRITE, 0, 1253244); intSum=0; intN; Longt1=System.currenttimemillis ();  for(inti=0;i<1253244;i++){                  //N=0x000000ff&buffer.get (i); //sum+=n;             }              LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }      }  }

Test results:

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

This shows that some or all of the files are mapped to memory after reading and writing, the speed will be much higher.

This is because the memory-mapped file first maps the file on the external memory to a contiguous area in memory, is treated as a byte array, the read-write operation directly to the memory, and then re-map the memory area to the external memory file, which saves the intermediate time to read and write, greatly reducing the read and write time.

Java uses memory mapping for large file uploads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.