Kio estas Memora mapado en java?

Memora mapado en java estas malalta nivela koncepto fari dosieron I/O pli efike. Ĝenerale ni uzas rivereton aŭ bufran rivereton fari registri 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. If you want to map the entire big file into memory then it might hang the system itself.

In the following example, we will use RandomAccessFile to read and write. Ni kreos dosieran kanalon kaj tiam uzi memoron mapanta mapi la dosieron kun memoro. Ĉi tie MappedByteBuffer estas rekta bufro esti uzita kun dosiero. Dum mapanta vin devas mencii la komencon kaj finan punkton aŭ legado aŭ skribo. Ĉi tiu tekniko helpas vin mapi apartan regionon de dosiero kaj elfari la dosieron I/O.

Ekzempla kodo:

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

Importi java.Io.*;
Importi java.Nio.*;
Importi java.Nio.Kanaloj.*;

/**
*
* @Aŭtoro kaushikp
*/
Publika klaso JavaMemoryMap {

/**
* @Param args la komandaj liniaj argumentoj
*/

Senmova int longeco = 2048; // 128 Mb
Publika senmova malplena ĉefa(Ŝnuro[] Args) Ĵetas Escepton {

//Krei RandomAccessFile kaj FileChannel
RandomAccessFile raf= nova RandomAccessFile(“Memorymaptest.Dat”, “Rw”);
FileChannel fc = raf.GetChannel();

//Mapanta la dosieron uzanta dosieran kanalon
MappedByteBuffer mbb1 = fc.Mapo(FileChannel.MapMode.READa_WRITE, 0, 1024);
MappedByteBuffer mbb2 = fc.Mapo(FileChannel.MapMode.READa_WRITE, 1024, 2048);

//Skribanta enhavon sur la mapita dosieron
Por(Int i = 0; I < 1024; I )
Mbb1.Metita((Byte)'X');
Sistemo.Ekstere.Println(“Finis skribanta xxx”);

Por(Int i = 0; I < 1024; I )
Mbb2.Metita((Byte)'Y');
Sistemo.Ekstere.Println(“Finis skribanta yyy”);
}
}

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

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share