Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share memory between two JVM instances?

Tags:

java

jvm

scala

I build a huge graph in JVM (Scala) which I want to use repeatedly, tweaking algorithms. I'd rather not reload it each time from disk. Is there a way to have it sit in one JVM while connecting from another, where the algorithms are being developed?

like image 270
Alexy Avatar asked Jul 28 '09 17:07

Alexy


People also ask

How do you communicate between two JVMs?

One java app can run the other via Runtime. exec() and control it (communicate) using the input stream. One could use JNI and a OS specific feature like named pipes or shared memory. You can also use java RMI to communicate between two applications, one calling methods of the other.

Does Java support shared memory?

You can share class data between Java™ Virtual Machines (JVMs) by storing it in a cache in shared memory. Sharing reduces the overall virtual storage consumption when more than one JVM shares a cache. Sharing also reduces the startup time for a JVM after the cache has been created.

What is shared memory in Java?

A shared memory connection allows both the client and the database to share the same memory on their host machine. This shared memory is temporary, and it is backed up by virtual memory. Shared memory connections are automatically used by Java applications running on the same machine as an InterSystems IRIS instance.

Is JVM shared?

The IBM® Java™ Virtual Machine (JVM) version 5 and higher allows you to share bootstrap and application classes between JVMs by storing them in a cache in shared memory.


2 Answers

Save your graph to disk, then map it into memory with MappedByteBuffer. Both processes should use the same memory, which will be shared with the page cache.

like image 127
bdonlan Avatar answered Sep 21 '22 13:09

bdonlan


Two JVMs sounds more complicated than it needs to be. Have you considered doing a kind of "hot deploy" setup, where your main program loads up the graph, displays the UI, and then asks for (or automatically looks for) a jar/class file to load that contains your actual algorithm code? That way your algorithm code would be running in the same jvm as your graph, but you wouldn't have to reload the graph just to reload a new algorithm implementation.

UPDATE to address OP's question in comment:

Here's how you could structure your code so that your algorithms would be swappable. It doesn't matter what the various algorithms do, so long as they are operating on the same input data. Just define an interface like the following, and have your graph algorithms implement it.

public interface GraphAlgorithm {
  public void doStuff(Map<whatever> myBigGraph)
}

If your algorithms are displaying results to some kind of widget, you could pass that in as well, or have doStuff() return some kind of results object.

like image 43
Peter Recore Avatar answered Sep 19 '22 13:09

Peter Recore