"In-depth study of Javanio 4" Memory mapped file I/O

Source: Internet
Author: User

Memory-mapped files allow you to create and modify files that are too large to fit into memory. With a memory-mapped file, you can assume that the file has all been read into memory, and then it is accessed as a very large array. This solution greatly simplifies the code to modify the file.
Filechannel.map (Filechannel.mapmode mode, long position, long size) maps the file area of this channel directly into memory. Note that you have to indicate where it starts mapping from where the file is, how big the map is, and that is, it can also map a small fragment of a large file.


Mappedbytebuffer is a subclass of Bytebuffer, so it has all the methods of Bytebuffer, but new force () forces the contents of the buffer to be flushed to the storage device, load () loads the data from the storage device into memory, IsLoaded () The data in memory is synchronized with the storage settings. In addition to the put () and get () methods, you can easily read and write basic type data by using methods such as Ascharbuffer () to get a buffered view of the corresponding basic type of data.

Memory-mapped file I/O is a way to read and write file data, which can be much faster than regular stream-based or channel-based I/O.

Memory-mapped file I/O is done by making the data in a file magically appear as the contents of a memory array. This may sound like an entire file being read into memory, but it's not in the first place. In general, only the portions of the file that are actually read or written are fed (or mapped ) into memory.

Memory mapping is not really magical or unusual. Modern operating systems typically map portions of a file to a portion of memory as needed to implement a file system. The Java memory mapping mechanism provides access to this mechanism only when it is possible to use this mechanism in the underlying operating system.

Although it is fairly straightforward to create a memory-mapped file, writing to it can be dangerous. Simply manipulating a single element of an array can directly modify the files on the disk. There is no separation between modifying the data and saving the data to disk.

(1) The best way to understand memory mapping is to use examples. In the example below, we are going to FileChannel map one (all or part of it) into memory. For this we will use the FileChannel.map() method. The following code lines map the first 1024 bytes of the file into memory:

 Public classusemappedfile{Static Private Final intStart = 0; Static Private Final intSize = 1024; Static  Public voidMain (String args[])throwsException {randomaccessfile RAF=NewRandomaccessfile ("Usemappedfile.txt", "RW" ); FileChannel FC=Raf.getchannel (); Mappedbytebuffer MBB=Fc.map (FileChannel.MapMode.READ_WRITE, start, size); Mbb.put (0, (byte) 97 ); Mbb.put (1023, (byte) 122 );  for(inti = start; i < size; i++) {System.out.print (Char) Mbb.get (i));  } raf.close (); }}

Operation Result:

(2) The program created a 128Mb file, if a one-time read to memory may lead to memory overflow, but the access here seems to be just a moment, this is because the real transfer into the memory is only a small part of it, the rest is placed on the Exchange file. This makes it easy for you to modify very large files (up to 2 GB). Note that Java is called the "file mapping mechanism" of the operating system to improve performance.

 Public classLargemappedfiles {Static intlength = 0x8000000;//Mb       Public Static voidMain (string[] args)throwsException {//to open the file in a readable and writable manner, use Randomaccessfile to create the file. FileChannel FC =NewRandomaccessfile ("Test.dat", "RW"). Getchannel (); //Note that the readable writable file channel is based on the file stream itself can be read and writtenMappedbytebuffer out = Fc.map (FileChannel.MapMode.READ_WRITE, 0, length); //Write 128M content         for(inti = 0; i < length; i++) {Out.put (byte) ' X '); } System.out.println ("Finished writing"); //read the middle of the file 6 bytes of content         for(inti = LENGTH/2; I < LENGTH/2 + 6; i++) {System.out.print (Char) Out.get (i));      } fc.close (); }  }  

Operation Result:

Finished writing
Xxxxxx

(3) Java processing large files, generally with bufferedreader,bufferedinputstream this kind of buffered IO class, but if the file is very large, the faster way is to use Mappedbytebuffer.

Three different ways:
FileChannel provides a map method for mapping files to memory image files: Mappedbytebuffer map (int mode,long position,long size); You can map the size of a file from position to a memory image file, mode indicates how the memory image file can be accessed: read_only,read_write,private.


A. Read_Only, (read-only): Attempting to modify the resulting buffer will cause the readonlybufferexception to be thrown. (mapmode.read_only)


B. Read_write (read/write): Changes to the resulting buffer will eventually propagate to the file; The change is not necessarily visible to other programs that map to the same file. (Mapmode.read_write)


C. Private (private): changes to the resulting buffer are not propagated to the file, and the change is not visible to other programs that map to the same file; instead, a private copy of the modified portion of the buffer is created. (mapmode.private)

Three methods:

A. Fore (); buffer is Read_write mode, this method forces the file to be written to the buffer content modification
B. Load () Loads the contents of the buffer into memory and returns a reference to the buffer
C. isLoaded () If the contents of the buffer are in physical memory, returns true, otherwise false

Three features:

After calling the channel's map () method, you can map part or all of the file to memory, the mapped memory buffer is a direct buffer and inherits from Bytebuffer, but it has more advantages over Bytebuffer:

A. Read fast
B. Write fast
C. Write Anywhere, anytime

 Packagestudy; ImportJava.io.FileInputStream; ImportJava.io.FileOutputStream; ImportJava.nio.ByteBuffer; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel;  Public classMapmemerybuffer { Public Static voidMain (string[] args)throwsException {bytebuffer bytebuf= Bytebuffer.allocate (1024 * 14 * 1024); byte[] bbb =New byte[14 * 1024 * 1024]; FileInputStream FIS=NewFileInputStream ("E://data/other/ultraedit_17.00.0.1035_sc.exe"); FileOutputStream Fos=NewFileOutputStream ("E://data/other/outfile.txt"); FileChannel FC=Fis.getchannel (); LongTimestar = System.currenttimemillis ();//get the current timeFc.read (BYTEBUF);//1 Read//Mappedbytebuffer MBB = Fc.map (FileChannel.MapMode.READ_ONLY, 0, Fc.size ()); System.out.println (Fc.size ()/1024); LongTimeend = System.currenttimemillis ();//get the current timeSystem.out.println ("Read Time:" + (Timeend-timestar) + "MS"); Timestar=System.currenttimemillis (); Fos.write (BBB);//2. Write//Mbb.flip (); Timeend =System.currenttimemillis (); System.out.println ("Write Time:" + (Timeend-timestar) + "MS");          Fos.flush ();          Fc.close ();      Fis.close (); }} Run Result:14235Read time:24ms Write time:21ms We commented out the annotations 1 and 2, replaced them with the annotated statement below, and then looked at the effect of the operation. 14235Read time:2ms Write time:0ms

It can be seen that the speed has been greatly improved. Mappedbytebuffer is fast, but there are some problems, such as memory footprint and file shutdown. Files opened by Mappedbytebuffer are closed only when garbage is collected, and this point is indeterminate. That's what it says in Javadoc: a mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is Garba Ge-collected.

Transferred from: http://langgufu.iteye.com/blog/2107023

"In-depth study of Javanio 4" Memory mapped file I/O

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.