`
leonzhx
  • 浏览: 786036 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Zz 10 Things to Know about Memory Mapped File in Java

    博客分类:
  • Java
阅读更多

What is Memory Mapped File and IO in Java

Memory mapped files are special, which allows Java program to access contents directly from memory, this is achieved by mapping whole file or portion of file into memory and operating system takes care of loading page requested and writing into file while application only deals with memory which results in very fast IO operations.Memory used to load Memory mapped file is outside of Java heap Space. Java programming language supportsmemory mapped file with java.nio package and has MappedByteBuffer to read and write from memory.

Advantage and Disadvantage of Memory MappedFile

Possibly the main advantage of Memory Mapped IO is performance which is important to build high frequency electronic trading system. Memory Mapped Files are way faster than standard file access via normal IO. Another big advantage of memory mapped IO is that it allows you to load potentially larger file which is not otherwise accessible. Experiments show that memory mapped IO performs better with large files. Though it has disadvantage in terms of increasing number of page faults. Since operating system only loads a portion of file into memory if a page requested is not present in memory, then it would result in page fault. It can also be used to share data between two processes.

Memory Mapped IO Support in Operating System

Most of the major operating systems like Windows platform, UNIX, Solaris and other UNIX like operating system supports memory mapped IO and with 64 bit architecture you can map almost any file into memory and access it directly using Java programming language.

Important Points of Memory Mapped IO in Java

Here are some of the important facts to know about Memory Mapped File in Java:

  1. Java supports Memory mapped IO with java.nio package.
  2. Memory mapped files is used in performance sensitive application, e.g. high frequency electronic trading platforms.
  3. By using memory mapped IO, you can load portion of large files in memory.
  4. Memory mapped file can result in page fault if requested page is not in memory.
  5. Ability to map a region of file in memory depends on addressable size of memory. In a 32 bit machine, you cannot access beyond 4GB or 2^32.
  6. Memory mapped IO is much faster than Stream IO in Java.
  7. Memory used to load File is outside of Java heap and reside on shared memory which allows two different processes to access File.
  8. Reading and writing on memory mapped file is done by operating system, so even if your Java program crashes after putting content into memory, it will make to File until OS is fine.
  9. Prefer Direct Byte buffer over Non Direct Buffer for faster performance.
  10. Don't call MappedByteBuffer.force() method too often, this method is meant to force operating system to write content of memory into disk, So if you call force() method each time you write into memory mappedfile, you will not see true benefit of using mapped byte buffer, instead it will be similar to disk IO.
  11. In case of power failure or host failure, there is slim chance that content of memory mapped file is not written into disk, which means you could lose critical data.

That's all folks. Memory mapped IO is an important concept for advanced Java developers, especially for writing high performance applications in Java. If you like this tip, then you may like to visit my bloghttp://javarevisited.blogspot.com for more such articles.

Thank you!

分享到:
评论

相关推荐

    MemoryMappedFile 使用 共享内存循环读写

    MemoryMappedFile 使用 C# 内存中分配一大块地址. 前边分配一个用与共同配置用 后边N个结构体 for循环建 一般用到此问题,两本程序 一个读一个写 循环读写 ----2015/09/24 Lyndon 上海----

    .NET4利用MemoryMappedFile实现跨进程多个可执行程序之间的通信

    基于.NET4.0的MemoryMappedFile实现共享内存通信 共享内存通信可以解决跨线程、跨进程、跨EXE之间通信的问题 对于需要传输图片等引用类数据,可以通过序列化和反序列化配合共享内存实现跨进程(跨EXE)通信 共享...

    Memory Mapped File Demo in WinCE

    总结来说,"Memory Mapped File Demo in WinCE"是一个关于如何在Windows CE系统中利用内存映射文件进行进程间数据交换的实践案例。开发者需要理解并掌握创建映射对象、获取视图、数据交换、同步保护以及释放资源等...

    C#进程间通信-共享内存代码实例

    在C#中,我们可以利用.NET Framework提供的MemoryMappedFile类来操作共享内存。MemoryMappedFile提供了创建、映射和访问内存映射文件的功能,这些文件可以在进程间共享。 首先,创建共享内存需要定义内存映射文件的...

    mmf4j:MemoryMappedFiles4Java

    MemoryMappedFiles4Java 描述 该库旨在将内存映射文件引入Java。 与已经存在的MappedByteBuffer相比,目标是更好地控制创建,修改和销毁。 它试图统一在不同操作系统上使用此类映射的接口,这意味着许多细节无法实现...

    基于AXI Memory Mapped To PCI Express IP核和RS485通信的FPGA设计

    在本FPGA设计中,AXI Memory Mapped To PCI Express IP核和RS485通信模块将协同工作,实现数据的高速传输和实时处理。 FPGA选择xc7k325tffg900-2 ,PCIe IP核选择AXI Memory Mapped To PCI Express,提供AXI4接口与...

    VS2017 C# 多进程通信之消息队列与大数据内存共享

    本程序为VS2017_C#多进程通信之消息队列与大数据内存共享,特别是内存共享部分,采用的是图片数据共享,既包含小数据(图片长宽)也包含大数据(图片),需要共享图片或数组等其他数据的可以参考。...

    MappedFile_source.zip_内存

    在标题"MappedFile_source.zip_内存"中,"MappedFile"通常指的是内存映射文件,这是一种在操作系统层面上将磁盘文件映射到进程内存空间的技术。这种方法允许程序直接访问文件内容,如同访问普通内存一样,极大地提高...

    Mapped File Class (4KB)

    标题 "Mapped File Class (4KB)" 提到的是一个关于映射文件类的实现,它涉及到计算机内存管理和操作系统层面的知识。映射文件(Mapped File)是一种技术,允许将磁盘上的文件内容直接映射到进程的虚拟地址空间,使得...

    Read-and-write-memory-mapped-file.rar_SLE4442_memory_映射内存 读写

    内存映射文件(Memory-Mapped File)是一种在操作系统中实现高效数据交换的技术,它允许将文件的内容直接映射到进程的虚拟地址空间中,使得文件的读写操作如同访问普通内存一样快速便捷。在Windows CE这样的嵌入式...

    .NET 4.0中使用内存映射文件实现进程间通讯

    重点展示如何使用.NET4.0中新增的MemoryMappedFile类实现进程间通讯,资源包中包含一个PDF文档,以及VS2010格式的三个示例文件。 与作者交流请访问 http://blog.csdn.net/bitfan/archive/2009/08/12/4438458.aspx 更...

    Memory-Mapped Files for Qualcomm Brew

    ### Memory-Mapped Files for Qualcomm Brew #### 作者:Ray Rischpater 在现代无线手持设备的开发过程中,内存管理始终是一项重要的考虑因素。尽管近年来移动设备的内存容量有了显著提升,但有效的内存管理和利用...

    numpy-numpy教程之MemorymappedFiles.zip

    在处理大量数据时,内存管理是一项关键任务,而NumPy中的Memory-mapped Files(内存映射文件)机制就是一个高效的数据处理工具。本文将深入探讨Memory-mapped Files在NumPy中的使用和其背后的原理。 Memory-mapped ...

    myDataExchange_内存共享_c#源代码_进程间数据交换_

    MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MyDataExchange", 1000); using (MemoryMappedViewStream stream = mmf.CreateViewStream()) { using (StreamWriter writer = new StreamWriter(stream)) { ...

    Direct Memory Access in Linux.doc

    1. **Using `/dev/mem`**: The simplest method is to use the `/dev/mem` device file, which provides access to the physical memory. However, this approach has limitations, as the memory accessed in this ...

    MDB: A Memory-Mapped Database and Backend for OpenLDAP

    本文主要介绍了一种名为MDB(Memory-Mapped Database)的读优化数据库库及其作为OpenLDAP后端的应用。该技术由Howard Chu开发,并在Symas Corp.以及OpenLDAP项目中得到应用。MDB作为一种专门为OpenLDAP设计的内存...

    Life After BerkeleyDB- OpenLDAP's Memory-Mapped Database

    - **Direct Access to Memory**: The entire database is mapped into virtual memory, and all data fetches are performed through direct access to the mapped memory instead of using intermediate buffers or...

    Scalable IO in Java doug lea.zip

    4. **File Channels**:专门用于文件I/O的通道,支持内存映射文件(Memory-Mapped Files),这是一种高效的文件读写方式,可以将文件直接映射到虚拟内存中。 5. **Pipe and Socket Channels**:提供进程间通信(IPC...

    Csharp内存映射文件包含源码

    内存映射文件(Memory-Mapped File)是C#编程中一种高效的数据访问技术,它允许程序将文件直接映射到进程的虚拟地址空间,从而能够像访问内存一样访问文件内容。这种方式在处理大文件时特别有用,因为它避免了频繁的...

    微软内部资料-SQL性能优化2

    Those pages may however have to be faulted in from a page file or memory mapped file. Backing Store Backing store is the physical representation of a memory address. Page Fault (Soft/Hard) A ...

Global site tag (gtag.js) - Google Analytics