Thinking in Java (memory mapped file)

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.

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  Blocke.printstacktrace ();} Long t=system.currenttimemillis ()-t1; System.out.println ("Sum:" +sum+ "  time:" +t);}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.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  Blocke.printstacktrace ();} Long t=system.currenttimemillis ()-t1; System.out.println ("Sum:" +sum+ "  time:" +t);}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.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 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.

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  Blocke.printstacktrace ();} Long t=system.currenttimemillis ()-t1; System.out.println ("Sum:" +sum+ "  time:" +t);}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.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  Blocke.printstacktrace ();} Long t=system.currenttimemillis ()-t1; System.out.println ("Sum:" +sum+ "  time:" +t);}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.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 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.

Ps: This article is only in my study to retrieve the relatively professional article, but has not been verified by the practice.


Thinking in Java (memory mapped file)

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.