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. Αν και ρυθμισμένο ρεύμα χρησιμοποιείται ευρέως για να αυξήσουν την απόδοση της εισόδου και εξόδου του αρχείου, αλλά χαρτογράφηση μνήμη είναι η απόλυτη τεχνική που μπορεί να χρησιμοποιηθεί για την αύξηση της απόδοσης δραστικά. Στο χάρτη μνήμης, το φυσικό αρχείο χαρτογραφείται με τη μνήμη και στη συνέχεια το I / O εκτελείται υπό την αντιστοιχισμένη μνήμη άμεσα. Ως αποτέλεσμα, η Ι / Ο γίνεται στο ίδιο το φυσικό αρχείο. Αλλά πρέπει να είστε προσεκτικοί σχετικά με τη χρήση της μνήμης. Είναι πάντα συνιστάται να χαρτογραφήσει τη μνήμη σε μικρά κομμάτια και στη συνέχεια ανάγνωσης / εγγραφής στο αρχείο. Αυξάνει την απόδοση και επίσης απελευθερώνει τη μνήμη μετά από κάθε χρήση. Αν θέλετε να χαρτογραφήσει ολόκληρο το μεγάλο αρχείο στη μνήμη, τότε μπορεί να κρεμάσει το ίδιο το σύστημα.

Στο ακόλουθο παράδειγμα, θα χρησιμοποιήσουμε RandomAccessFile να διαβάσει και να γράψει. 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
δημόσια στατική άκυρη κύρια(Κορδόνι[] args) throws Exception {

//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 i = 0; εγώ < 1024; i )
mbb1.put((byte)’x’);
System.out.println(“Ολοκληρώθηκε xxx εγγράφως”);

για(int i = 0; εγώ < 1024; i )
mbb2.put((byte)«Και»);
System.out.println(“Ολοκληρώθηκε yyy εγγράφως”);
}
}

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

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share