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. Ако искате да картографира цялата голям файл в паметта то това би могло виси самата система.

В следващия пример, ние ще използваме RandomAccessFile да четат и пишат. Ние ще създадете файл канал и след това използвайте картографиране памет към картата с памет файла. Тук MappedByteBuffer е директен буфер, за да се използва с файл. Докато картографиране трябва да говорим за начало и край точката или четене или писане. Тази техника помага да се карта определен регион на даден файл и да изпълнява файла I / O.

Примерен код:

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

внос java.io. *;
внасят java.nio. *;
java.nio.channels внос. *;

/**
*
* author kaushikp
*/
обществени клас JavaMemoryMap {

/**
* param аргументи аргументите на командния ред
*/

статична дължина инт = 2048; // 128 Mb
публично статично невалидни основни(Низ[] опцията) хвърля Exception {

//Създайте RandomAccessFile и FileChannel
RandomAccessFile Raf = нова RandomAccessFile(“memorymaptest.dat”, “RW”);
FileChannel ев = raf.getChannel();

//Картографиране на файлове използвайте файл канал
MappedByteBuffer BB1 = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
MappedByteBuffer BB2 = fc.map(FileChannel.MapMode.READ_WRITE, 1024, 2048);

//Писане на съдържание на картирани файл
за(Int I = 0; аз < 1024; I )
mbb1.put((байт)"X");
System.out.println(“Завършил написването ххх”);

за(Int I = 0; аз < 1024; I )
mbb2.put((байт)"А");
System.out.println(“Завършил написването ууу”);
}
}

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

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share