shared memory in java

Q

qwejohn

Hello,

Is there someting which may be termed as "shared memory in java" ?
(I mean something which may connect 2 Java processes but is not a
socket ;
something like CreateFileMapping() in windows or shmget/shmat() (Unix
System V IPC) or
shm_open() (Unix Posix IPC).

Googling for "shared memory in java" in groups gave 7 results which did
not
really satisfy me.

Regards,
John
 
V

Vincent Cantin

Hello,
Is there someting which may be termed as "shared memory in java" ?
(I mean something which may connect 2 Java processes but is not a
socket ;
something like CreateFileMapping() in windows or shmget/shmat() (Unix
System V IPC) or
shm_open() (Unix Posix IPC).

In java, the easier way to share the memory between 2 programs is to use 2
threads in 1 JVM.
Moreover, the java language contains a built-in mechanism to access to the
objects and their data wrt the concurrent threads (the "synchronized"
keyword). In C++ it was hairy, in Java it is smooth and easy.

Vincent Cantin
 
B

Bozo Bits

In general you have to use C/C++ and JNI to create a shared memory
segment. Then there is the issues of moving object state into and out
of memory, this can be pretty complex to do and get it right. Most of
the time you can design around the problem with RMI or standard network
programming.

The other route is to use a commercial product such as Gemstone Systems
Facets or Gemfire product lines which have already solved the shared
memory issues..

V
 
G

Gordon Beaton

Is there someting which may be termed as "shared memory in java" ?
(I mean something which may connect 2 Java processes but is not a
socket ; something like CreateFileMapping() in windows or
shmget/shmat() (Unix System V IPC) or shm_open() (Unix Posix IPC).

Not really the same as shared memory, but maybe Java Spaces would
provide a more Java-like solution to whatever it is you're trying to
do.

/gordon
 
T

timjowers

I thought maybe RandomAccessFile might be a memory mapped file but JB
pointed out this is not so in the thread: "How to initialize a big
(String-)Array fast?". I have not found a memory mapped file class in
Java. What would be nice is a RAMFile and if this same memory can be
accessed by name from CreateFile(). That would be nice.

This would be a very good assignment for a graduate level Java class:
"Create a Java RAM File object that uses native APIs to provide a
memory mapped file."
 
J

John C. Bollinger

I thought maybe RandomAccessFile might be a memory mapped file but JB
pointed out this is not so in the thread: "How to initialize a big
(String-)Array fast?". I have not found a memory mapped file class in
Java.

You would want to consider the java.nio.MappedByteBuffer class, which
does represent a view of a memory-mapped file.
 
B

Bozo Bits

One thing to keep in mind is whether it can be shared between
processes, which I don't think you can do with
java.nio.MappedByteBuffer. The other issue with this is that for small
files it probably isn't an issue but large files will consume a
significant amount of the virtual address space that an application may
need( same issues exist for users of shared memory).
 
Q

qwejohn

Hello,
Thanks;
I am familiar and had used many times concurrent threads and
"synchronized" in Java.

The thing is , I want to share memory between 2 threads/processes , one
in Java and one in "C".

There is of course the JNI solution; I am currently using it; the thing
is that when you call
a JNI method from Java , and there is some problem in the "C" code
(like a pointer which is not handled right and ,for example, is
incremented
so that it points to a non-valid address) it is sometimes difficult to
trace. Of course you can use bound checkers and other memory debugging
tools,
but it is a sometimes cumbersome. Especially , when there is a problem
with a pointer as I depicted above, the crash will be sometiomes in the

Java part (so it seems to me ) ; As I understand , when you allocate
memory
in the JNI "C" method, it gets it memory from a memory manager of
the "c" compiler , but there is an upper memory layer of Java which
controls it somehow. (I am NOT sure about it; memory managers
allocatiOn algorithms are complex , and I don't know much about Java
memory manager
since Sun's code is not an open source ).

If the two threads need only a little in common (lets say one should
only
pass a string of 10 chars to the second ("C") code, as it is in my case
, it seems to me
that using shared memory can be a solution (if it is an option at all).


It may be a little less efficemt than using JNI, but it can be easier
to maintain/debug.

Regards,
John
 
V

Vincent Cantin

One thing to keep in mind is whether it can be shared between
processes, which I don't think you can do with
java.nio.MappedByteBuffer.

This is a abstract class, not final, so you can redefine the implementation
of the functions in a subclass written by your own (.. using JNI to access
the shared memory segment).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top