Allocating memory outside JVM and using it inside JVM

后端 未结 2 549
清歌不尽
清歌不尽 2021-02-13 18:28

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?

Particular idea i

相关标签:
2条回答
  • 2021-02-13 19:24

    Yes, this is completely possible, even without JNI.

    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.

    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.

    0 讨论(0)
  • 2021-02-13 19:33

    You can use MappedByteBuffer yourself or you can use a data structure built on top of a MappedByteBuffer so it can be available on restart and even shared between JVMs.

    Chronicle-Map has a key-value store modelled as a ConcurrentMap. e.g. Map<String, YourType>

    Chronicle-Queue is a journal of every event in your system e.g. a log you can consume in real-time.

    These are both open source and free and save you having to work out how to store and retrieve objects from a persisted store.

    Note: as these are off-heap and persisted, they can be TBs in size without impacting the GC pauses times.

    0 讨论(0)
提交回复
热议问题