Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Share memory between C and Java

Reply
Thread Tools

Share memory between C and Java

 
 
Mironov Leonid
Guest
Posts: n/a
 
      07-27-2003
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
 
Reply With Quote
 
 
 
 
Shripathi Kamath
Guest
Posts: n/a
 
      07-27-2003
"Mironov Leonid" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


--
Shripathi Kamath
NETAPHOR SOFTWARE INC.
http://www.netaphor.com


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-27-2003
On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
<(E-Mail Removed)> wrote or quoted :

> jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);


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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Mark Thornton
Guest
Posts: n/a
 
      07-27-2003
Roedy Green wrote:

> On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
> <(E-Mail Removed)> wrote or quoted :
>
>
>>jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);

>
>
> 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

 
Reply With Quote
 
Joseph Millar
Guest
Posts: n/a
 
      07-28-2003
On Sun, 27 Jul 2003 22:01:10 +0100, Mark Thornton <(E-Mail Removed)>
wrote:
> Roedy Green wrote:
>
> > On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
> > <(E-Mail Removed)> wrote or quoted :
> >
> >
> >>jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);

> >
> >
> > 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).


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
 
Reply With Quote
 
Peter Szymanski
Guest
Posts: n/a
 
      07-28-2003
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.

Mironov Leonid wrote:

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
a question about "memory share" between java and c Ejeep Java 2 11-08-2007 05:32 AM
how to share data/memory between mutiple JVMs ? Jeff.C Java 7 07-26-2006 06:36 AM
share function argument between subsequent calls but not between class instances! K. Jansma Python 7 02-21-2006 01:31 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM



Advertisments