Any concept of shared memory in Java

2019-01-06 16:39发布

AFAIK, memory in Java is based on heap from which the memory is allotted to objects dynamically and there is no concept of shared memory.

If there is no concept of shared memory, then the communication between Java programs should be time consuming. In C where inter-process communication is quicker via shared memory compared to other modes of communication.

Correct me if I'm wrong. Also what is the quickest way for 2 Java progs to talk to each other.

10条回答
放荡不羁爱自由
2楼-- · 2019-01-06 16:51

There's a couple of comparable technologies I can think of:

  1. A few years back there was a technology called JavaSpaces but that never really seemed to take hold, a shame if you ask me.
  2. Nowadays there are the distributed cache technologies, things like Coherence and Tangosol.

Unfortunately neither will have the out right speed of shared memory, but they do deal with the issues of concurrent access, etc.

查看更多
冷血范
3楼-- · 2019-01-06 17:01

MappedBus (http://github.com/caplogic/mappedbus) is a library I've added on github which enable IPC between multiple (more than two) Java processes/JVMs by message passing.

The transport can be either a memory mapped file or shared memory. To use it with shared memory simply follow the examples on the github page but point the readers/writers to a file under "/dev/shm/".

It's open source and the implementation is fully explained on the github page.

查看更多
贪生不怕死
4楼-- · 2019-01-06 17:03

Similar to Peter Lawrey's Java Chronicle, you can try Jocket.

It also uses a MappedByteBuffer but does not persist any data and is meant to be used as a drop-in replacement to Socket / ServerSocket.

Roundtrip latency for a 1kB ping-pong is around a half-microsecond.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-06 17:05

There are at least 2 ways to do it - RAM Drive or Apache APR.

Details here and here with some performance measurements.

查看更多
我只想做你的唯一
6楼-- · 2019-01-06 17:05

Peter Lawrey's Java Chronicle project is worth looking at.

These are some tests that I had done a while ago comparing various off-heap and on-heap options.

查看更多
Animai°情兽
7楼-- · 2019-01-06 17:05

The information provided by Cowan is correct. However, even shared memory won't always appear to be identical in multiple threads (and/or processes) at the same time. The key underlying reason is the Java memory model (which is built on the hardware memory model). See Can multiple threads see writes on a direct mapped ByteBuffer in Java? for a quite useful discussion of the subject.

查看更多
登录 后发表回答