Any concept of shared memory in Java

后端 未结 10 503
猫巷女王i
猫巷女王i 2020-11-29 05:33

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 s

相关标签:
10条回答
  • 2020-11-29 05:41

    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.

    0 讨论(0)
  • 2020-11-29 05:45

    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.

    0 讨论(0)
  • 2020-11-29 05:46

    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.

    0 讨论(0)
  • 2020-11-29 05:55

    One thing to look at is using memory-mapped files, using Java NIO's FileChannel class or similar (see the map() method). We've used this very successfully to communicate (in our case one-way) between a Java process and a C native one on the same machine.

    I'll admit I'm no filesystem expert (luckily we do have one on staff!) but the performance for us is absolutely blazingly fast -- effectively you're treating a section of the page cache as a file and reading + writing to it directly without the overhead of system calls. I'm not sure about the guarantees and coherency -- there are methods in Java to force changes to be written to the file, which implies that they are (sometimes? typically? usually? normally? not sure) written to the actual underlying file (somewhat? very? extremely?) lazily, meaning that some proportion of the time it's basically just a shared memory segment.

    In theory, as I understand it, memory-mapped files CAN actually be backed by a shared memory segment (they're just file handles, I think) but I'm not aware of a way to do so in Java without JNI.

    0 讨论(0)
  • 2020-11-29 05:59

    Shared memory is sometimes quick. Sometimes its not - it hurts CPU caches and synchronization is often a pain (and should it rely upon mutexes and such, can be a major performance penalty).

    Barrelfish is an operating system that demonstrates that IPC using message passing is actually faster than shared memory as the number of cores increases (on conventional X86 architectures as well as the more exotic NUMA NUCA stuff you'd guess it was targeting).

    So your assumption that shared memory is fast needs testing for your particular scenario and on your target hardware. Its not a generic sound assumption these days!

    0 讨论(0)
  • 2020-11-29 06:02

    The easiest way to do that is to have two processes instantiate the same memory-mapped file. In practice they will be sharing the same off-heap memory space. You can grab the physical address of this memory and use sun.misc.Unsafe to write/read primitives. It supports concurrency through the putXXXVolatile/getXXXVolatile methods. Take a look on CoralQueue which offers IPC easily as well as inter-thread communication inside the same JVM.

    Disclaimer: I am one of the developers of CoralQueue.

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