在JVM外部分配内存并在JVM中使用它 [英] Allocating memory outside JVM and using it inside JVM

查看:162
本文介绍了在JVM外部分配内存并在JVM中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JVM内存之外创建一个持久性内存对象,可以在JVM中作为对象使用,以便它能够在JVM重启后继续存在?

Is it possible to create a persistent memory object outside JVM memory that can be used inside the JVM as an object, so that it survives a JVM restart?

特殊我们的想法是,我们可以在JVM之外分配内存,然后使用JNI接口来访问这个内存,并将一些Java数组与它相关联。

Particular idea is that we can allocate memory outside the JVM and then use a JNI interface to access this memory and associate, say, some Java array with it.

有人尝试过执行这样的黑客?任何平台依赖都足够了。

Did somebody try to perform such hack? Any platform dependency would suffice.

例如,这有助于在重启JVM进程期间执行内存数据库加载的优化。

For example, this can help to perform optimization of in-memory DB loading during restart of the JVM process.

推荐答案

是的,这是完全可能的,即使没有JNI也是如此。

Yes, this is completely possible, even without JNI.

我的想法是由 MappedByteBuffer ://en.wikipedia.org/wiki/Tmpfs> tmpfs 文件系统。例如。在Linux上你可以使用 / dev / shm (或 / run / shm )mountpoint。

The idea is to have a MappedByteBuffer backed by a "file" on tmpfs filesystem. E.g. on Linux you can use /dev/shm (or /run/shm) mountpoint for that.

这样的MappedByteBuffer的性能与其他Direct ByteBuuff的性能相同,但它会持续JVM重启,即你可以在新的JVM中再次映射这个文件。 (我在引号中写文件,因为它看起来像应用程序的常规文件,但它实际上是驻留在RAM中的共享内存区域)。我们积极地将这种技术用于生产内存缓存。

The performance of such MappedByteBuffer will be the same as for other Direct ByteBuffers, but it will persist the JVM restart, i.e. you can map this "file" again in a new JVM. (I write "file" in quotes, because it looks like a regular file for application, but it is actually a shared memory region that resides in RAM). We actively use this technique for our production in-memory caches.

这篇关于在JVM外部分配内存并在JVM中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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