What is Memory mapping in java?

Memory mapping in java is a low level concept to make file I/O more efficiently. Generally we use stream or buffer stream to do file I/O. Although buffered stream is widely used to increase the performance of file input and output, but memory mapping is the ultimate technique that can be used to increase the performance drastically. In memory map, the physical file is mapped with the memory and then the I/O is performed in the mapped memory directly. As a result the I/O is performed on the physical file itself. But you need to be careful about the memory usage. It is always recommended to map the memory in small chunks and then read/write on the file. It increases the performance and also releases the memory after use. As jy wil die hele groot lêer in die geheue kaart dan is dit dalk die stelsel self hang.

In die volgende voorbeeld, Ons sal RandomAccessFile gebruik om te lees en skryf. Ons sal 'n lêer kanaal te skep en gebruik dan die geheue kartering van die lêer met die geheue kaart. Hier MappedByteBuffer is 'n direkte buffer om gebruik te word met lêer. Terwyl kartering jy moet die begin- en eindpunt melding of lees of skryf. Hierdie tegniek help jou om te karteer 'n bepaalde streek van 'n lêer en die lêer uit te voer I / O.

monster-kode:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package JavaTips.com;

invoer java.io. *;
invoer java.nio. *;
invoer java.nio.channels. *;

/**
*
* @ author kaushikp
*/
openbare klas JavaMemoryMap {

/**
* param args die command line argumente
*/

statiese int lengte = 2048; // 128 MB
openbare statiese nietig hoof(String[] argumente) gooi Uitsondering {

//Skep RandomAccessFile en FileChannel
RandomAccessFile RAF = nuwe RandomAccessFile(“memorymaptest.dat”, “RW”);
FileChannel FC = raf.getChannel();

//Mapping the file using file channel
MappedByteBuffer mbb1 = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
MappedByteBuffer mbb2 = fc.map(FileChannel.MapMode.READ_WRITE, 1024, 2048);

//Writing content on the mapped file
vir(int i = 0; i < 1024; i )
mbb1.put((byte)’x’);
System.out.println(“Finished writing xxx”);

vir(int i = 0; i < 1024; i )
mbb2.put((byte)’y’);
System.out.println(“Finished writing yyy”);
}
}

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share