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. למרות זרם שנאגר נעשה שימוש נרחב כדי להגביר את הביצועים של קלט ופלט לקובץ,,en,אבל מיפוי זיכרון היא הטכניקה האולטימטיבית שיכול לשמש כדי לשפר את הביצועים באופן דרסטי,,en,בשנת מפת הזיכרון,,en,הקובץ הפיזי ממופה עם הזיכרון ואז O / I מתבצע בזיכרון הממופה ישירות,,en,כתוצאה מכך O / I מבוצע על הקובץ הפיזי עצם,,en,אבל אתה צריך להיות זהיר לגבי השימוש בזיכרון,,en,מומלץ תמיד למפות את הזיכרון בגושים קטנים ולאחר מכן לקרוא / לכתוב על הקובץ,,en,זה מגדיל את הביצועים וגם משחרר את הזיכרון לאחר שימוש,,en,אם אתה רוצה למפות את הקובץ הגדול כולו לזיכרון אז זה יכול לתלות את המערכת עצמה,,en,בדוגמא הבאה,,en,נשתמש RandomAccessFile לקרוא ולכתוב,,en, 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. We will create a file channel and then use memory mapping to map the file with memory. Here MappedByteBuffer is a direct buffer to be used with file. While mapping you must mention the start and end point or reading or writing. This technique helps you to map a particular region of a file and perform the file I/O.

Sample code:

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

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

/**
*
* @author kaushikp
*/
public class JavaMemoryMap {

/**
* @param args the command line arguments
*/

static int length = 2048; // 128 Mb
הריק סטטי הראשי ציבורי(חוט[] ארגומנטים) זורק חריגה {

//Create RandomAccessFile and FileChannel
RandomAccessFile raf= new 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
עבור(אני int = 0; אני < 1024; אני )
mbb1.put((byte)’x’);
שיטה(“Finished writing xxx”);

עבור(אני int = 0; אני < 1024; אני )
mbb2.put((byte)"ו",,es,yyy כתיבה סיים,,en,מיפוי זיכרון ב- Java,,en);
שיטה(“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