java – 在Android下取消映射或“释放”MappedByteBuffer

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));
        }
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...