Share memory between C and Java

M

Mironov Leonid

Guys , hi !

I have the following problem :

i want to create something similar to windows' FileMapping.
I have an application in C that had allocated some memory buffer and
now it writes to this buffer every once in a while.

I want this buffer to be available for reading from JAVA applications,
but :
since performance is really an issue here, i don't want to issue a jni
call
each time i want my java application needs to access the buffer, (even
if the C application can fire some kind of event)

I would like the java app. call the jni method once, get the shared
memory location and be able to treat it as a buffer as well - with
changes appearing directly as they are seen to the C program.
(For example the Java app. could spin loop on some location in the
shared memory and act as soon the value changes...)

Is there a conceptual problem to accomplish my mission ?
If not ,how can i achieve that ?
thanks in advance.

Lenny
 
S

Shripathi Kamath

Mironov Leonid said:
Guys , hi !

I have the following problem :

i want to create something similar to windows' FileMapping.

J2SE 1.4 has support for memory mapped files.
I have an application in C that had allocated some memory buffer and
now it writes to this buffer every once in a while.

I want this buffer to be available for reading from JAVA applications,
but :
since performance is really an issue here, i don't want to issue a jni
call
each time i want my java application needs to access the buffer, (even
if the C application can fire some kind of event)

The JNI plays it safer, and does not let the Java side directly access
memory. It may work, but certainly is not advertised in that fashion.
I would like the java app. call the jni method once, get the shared
memory location and be able to treat it as a buffer as well - with
changes appearing directly as they are seen to the C program.

You *can* attempt this, and it might work, but only because of specific
implementation, not by specification.
(For example the Java app. could spin loop on some location in the
shared memory and act as soon the value changes...)
That alone will cause you a lot of performance issues
Is there a conceptual problem to accomplish my mission ?

It is unclear. You have expressed your desire to solve a problem in a
particular way. The way in which you want to solve it is clear, the problem
itself is not.

What are the constraints on performance? What are you seeing when you use
JNI as advertised?
If not ,how can i achieve that ?
thanks in advance.

Please describe the problem completely. That can help you get better
answers.
 
M

Mark Thornton

Roedy said:
what would be the advantage of this over a ordinary byte array?

The data isn't moved in memory by the garbage collector (it doesn't come
out of the heap but is separately allocated from system memory).

Mark Thornton
 
J

Joseph Millar

The data isn't moved in memory by the garbage collector (it doesn't come
out of the heap but is separately allocated from system memory).

Yep. A byte array in Java is an object and the JVM can move
it around depending on its own needs. The JNI api allows
the native access of a Java array once it's pinned, but the
opposite has not been true until 1.4. The new api will give
you the ability to create a Java array with a backing store
that is allocated from somewhere other than the Java heap or
stack. I can see it being extremely useful in embedded
environments and other places where Java must interface with
outside code.

--Joe
 
P

Peter Szymanski

Hi,
Im a bit of newbie, but I have also recently been trying this stuff for
image acquisition, I am having alot of performance issues with the
BufferedImage.

<c file>
JNIEXPORT jobject JNICALL Java_CameraEpixJNI_doNativeMapBuffer
(JNIEnv *env, jobject obj, jint cardNum, jint bufNum){
...
//(membuffer malloced earlier)
return (*env)->NewDirectByteBuffer(env, membuf, capacity);
....

}
</c>
<java>
this.frame = doNativeMapBuffer(this.epixCardNumber, ++frameBuf);//Frame is
ByteBuffer
</java>
currently in java, whenever the application needs the BufferedImage, I use
the 'bulk get' ByteBuffer.get(byte[] into, offset, length) method, where
the into[] array has been used to create the member BufferedImage variable
in the constructor of the camera class.

By the way,
does anyone know if the BufferedImage can be 'wrapped' around the
DirectByteBuffer? to get rid of the copying?

Regards,
Peter


regards,
Peter.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top