FileChannel and Mappedbytebuffer

Source: Internet
Author: User


Files can be accessed efficiently using memory-mapped files. The following code makes an example of comparing the operation of a memory-mapped file with the efficiency difference of a fileoutputstream write file.


Import Java.io.File;

Import java.io.FileNotFoundException;

Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.io.RandomAccessFile;

Import Java.nio.MappedByteBuffer;

Import Java.nio.channels.FileChannel;

Import Java.nio.channels.FileChannel.MapMode;


Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;


public class Fileclient {

Private FileChannel FileChannel;

Private Mappedbytebuffer Mappedbytebuffer;

private file file;


private static int mapedfilesizecommitlog = 1024 * 1024 * 1024;


private static final String FileName = "D:\\temp\\testfilecache\\a.dat";

private static final String fileName2 = "D:\\temp\\testfilecache\\b.dat";


private static final Logger Logger = Loggerfactory.getlogger (FileClient.class.getName ());


public static void Main (string[] args) throws IOException {

Fileclient fclient = new Fileclient ();

Fclient.start ();

Fclient.start2 ();

}


@SuppressWarnings ("resource")

public void Start () throws IOException {

File = new file (fileName);

String teststring = "Hello";

Boolean OK = false;

try {

This.filechannel = new Randomaccessfile (this.file, "RW"). Getchannel ();

This.mappedbytebuffer = This.fileChannel.map (mapmode.read_write, 0, Mapedfilesizecommitlog);

int curpos = 0;

Long startTime = System.currenttimemillis ();

for (int i = 0; i < 1000000; i++) {

This.mappedByteBuffer.position (CurPos);

This.mappedByteBuffer.put (Teststring.getbytes ());

CurPos + = 6;

}

This.mappedByteBuffer.force ();

This.fileChannel.close ();

Long stoptime = System.currenttimemillis ();

Logger.debug ("Time:" + (Stoptime-starttime));

OK = true;

} catch (FileNotFoundException e) {

Logger.error ("Create file Channel" + FileName + "Failed.", e);

Throw e;

} catch (IOException e) {

Logger.error ("Map file" + fileName + "Failed.", e);

Throw e;

} finally {

if (!ok && this.filechannel! = null) {

This.fileChannel.close ();

}

}

}


public void Start2 () throws IOException {

FileOutputStream fos = new FileOutputStream (fileName2);

String teststring = "Hello";


Long startTime = System.currenttimemillis ();

for (int i = 0; i < 1000000; i++) {

Fos.write (Teststring.getbytes ());

}

Fos.close ();

Long stoptime = System.currenttimemillis ();

Logger.debug ("Time2:" + (Stoptime-starttime));


}


}


Server SATA disk, write performance:

[[email protected] ~]# dd If=/dev/zero of=kwxgd bs=64k count=4k Oflag=dsync

4096+0 Records in

4096+0 Records out

268435456 bytes (268 MB) copied, 8.93065 seconds, 30.1 MB/s

After running, you can see the results of the comparison:

[Email protected] ~]# Java-jar Filechannel-test.jar

17:56:46.987 [main] DEBUG o.l.filechannel_test. fileclient-time:207

17:56:48.670 [main] DEBUG o.l.filechannel_test. fileclient-time2:1675

8 Times Times Performance Difference


The code can be downloaded from https://github.com/mfcliu/netty-learning.




This article from "Qin like Spring Bud" blog, please be sure to keep this source http://leo01.blog.51cto.com/694903/1738057

FileChannel and Mappedbytebuffer

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.