在Android下取消映射或“释放" MappedByteBuffer [英] Unmapping or 'release' a MappedByteBuffer under Android

查看:298
本文介绍了在Android下取消映射或“释放" MappedByteBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的常见问题是您具有此处针对14岁的错误报告;)

The usual problem in Java is that you have to hack to get a proper unmapping of memory mapped files - see here for the 14year old bug report ;)

但是在Android上,仅通过NDK似乎有0个纯Java解决方案.这是真的?如果是,是否有指向具有Android/Java绑定的开源解决方案的指针?

But on Android there seems to be 0 solutions in pure Java and just via NDK. Is this true? If yes, any pointers to an open source solution with Android/Java bindings?

推荐答案

Android下没有可用的黑客工具.

There is no hack available under Android.

但是,有一些帮助程序和代码片段使mmap文件的C-Java绑定变得容易/容易:

But there are a few helpers and snippets which make the C-Java binding for mmap files easy/easier:

  • util-mmap, Apache License 2.0, here is an issue regarding Android support
  • Using Memory Mapped Files and JNI to communicate between Java and C++ programs or easier with tools like javacpp?
  • It looks tomcat has implement a helper (jni.MMap) that is able to unmap/delete a mmap file

查看 util-mmap ,非常简单:

public class MMapTesting {

    public static void main(String[] args) throws IOException {
        File file = new File("test");
        MMapBuffer buffer = new MMapBuffer(file, 0, 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, FileChannel.MapMode.READ_WRITE, 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下取消映射或“释放" MappedByteBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆