Share memory between C and Java

Discussion in 'Java' started by Mironov Leonid, Jul 27, 2003.

  1. 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
     
    Mironov Leonid, Jul 27, 2003
    #1
    1. Advertising

  2. "Mironov Leonid" <> wrote in message
    news:...
    > 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
     
    Shripathi Kamath, Jul 27, 2003
    #2
    1. Advertising

  3. Mironov Leonid

    Roedy Green Guest

    On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
    <> 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.
     
    Roedy Green, Jul 27, 2003
    #3
  4. Roedy Green wrote:

    > On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
    > <> 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
     
    Mark Thornton, Jul 27, 2003
    #4
  5. On Sun, 27 Jul 2003 22:01:10 +0100, Mark Thornton <>
    wrote:
    > Roedy Green wrote:
    >
    > > On Sun, 27 Jul 2003 19:29:50 GMT, Joseph Millar
    > > <> 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
     
    Joseph Millar, Jul 28, 2003
    #5
  6. 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
     
    Peter Szymanski, Jul 28, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. K. Jansma
    Replies:
    7
    Views:
    338
    Duncan Booth
    Feb 21, 2006
  2. Jeff.C
    Replies:
    7
    Views:
    1,507
    Ingo R. Homann
    Jul 26, 2006
  3. Ejeep
    Replies:
    2
    Views:
    340
    Ejeep
    Nov 8, 2007
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,427
    Saraswati lakki
    Jan 6, 2012
  5. Jack
    Replies:
    2
    Views:
    246
Loading...

Share This Page