Java中通常的问题是您有
to hack可以正确取消映射内存映射文件 – 请参阅
here for 14年代的错误报告;)
但是在Android上,纯Java中似乎只有0个解决方案,只是通过NDK.这是真的?如果是,任何指向使用Android / Java绑定的开源解决方案的指针?
解决方法
Android下没有黑客可用.
但是有一些帮助器和代码片段使得对于mmap文件的C-Java绑定变得容易/容易:
> util-mmap,Apache License 2.0,here是有关Android支持的问题
> Using Memory Mapped Files and JNI to communicate between Java and C++ programs或更简单的工具,如javacpp?
看起来tomcat有实现a helper (jni.MMap),能够取消/删除一个mmap文件
看到util-mmap在行动,真的很容易:
- public class MMapTesting {
- public static void main(String[] args) throws IOException {
- File file = new File("test");
- MMapBuffer buffer = new MMapBuffer(file,1000,FileChannel.MapMode.READ_WRITE,ByteOrder.BIG_ENDIAN)) {
- buffer.memory().intArray(0,100).set(2,234);
- // calls unmap under the hood
- buffer.close();
- // here we call unmap automatically at the end of this try-resource block
- try (MMapBuffer buffer = new MMapBuffer(file,ByteOrder.BIG_ENDIAN)) {
- System.out.println("length: " + buffer.memory().length());
- IntArray arr = buffer.memory().intArray(0,buffer.memory().length() / 8);
- // prints 234
- System.out.println(arr.get(2));
- }
- }
- }