Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating memory outside JVM and using it inside JVM

Tags:

java

memory

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?

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.

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

like image 940
Andremoniy Avatar asked Feb 06 '16 13:02

Andremoniy


People also ask

How the memory is allocated inside JVM?

Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures. The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space.

What is object and how does the JVM allocate memory to it?

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on the heap (See this for more details).

Where is the memory allocated for the JVM?

The JVM allocates Java heap memory from the OS and then manages the heap for the Java application. When an application creates a new object, the JVM sub-allocates a contiguous area of heap memory to store it.


2 Answers

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.

like image 80
apangin Avatar answered Nov 15 '22 17:11

apangin


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.

like image 24
Peter Lawrey Avatar answered Nov 15 '22 19:11

Peter Lawrey