Allow native code to directly access objects in java heap?

I have an array of objects in Java and I want to allow native C code direct access to that array (no copying or accessor functions). Is this possible? I don't mind if the solution is JVM-specific.


of course it is possible with JNI.

It's worth consulting this link : in short -

#include <jni.h>
#include "IntArray.h"

Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
  jsize len = (*env)->GetArrayLength(env, arr);
  int i, sum = 0;
  jint *body = (*env)->GetIntArrayElements(env, arr, 0);
  for (i=0; i<len; i++)
    sum += body[i];
  (*env)->ReleaseIntArrayElements(env, arr, body, 0);
  return sum;

If you're using newer versions of java, use ByteBuffer objects.

Call ByteBuffer.allocateDirect() to allocate the buffer. The direct buffer lies outside the garbage collector's domain. To access the buffer from JNI, call GetDirectBufferAddress(). It returns a pointer to the byte buffer. This will do no copying under the hood. The changes to the buffer will be seen on the Java and Native side.

The javadocs have some warnings about using direct buffers:

A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious. It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations. In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.

I finally found an answer. It is generally not possible to do what I wanted with JNI. However, some VMs provide the needed functionality, I used JikesRVM:

1) The native code needs the memory location of the array. This can be achieved using the 'Magic' facility in JikesRVM, it provides an ObjectReference and an Address class that allow to obtain a memory address for each object. This address can then be forwarded to the native code as long/int argument (using JNI) and there casted to a pointer.

2) The GC may not move objects around. This is a bit more tricky since it requires support for pinning in the GC. In the case of JikesRVM, objects can be annotated with @NonMoving and @NonMovingAllocation (also part of 'Magic'). Furthermore, objects (i.e. arrays) > 8KB are placed in a Large Object Space, which doesn't move objects around.

All of above question, there is a good solution(click below link):


You can find how u can provide direct access if you have an object instead of primitive array.

Need Your Help

jQuery draggable list with clickable objects within - preventing click on drag

jquery binding click draggable

I have a ul that is draggable, and there are anchors in the li's that I want to deactivate during the drag so that they're not triggered once the draggable.stop() event triggers.

keyboard shortcut for mouse right click

windows-7 keyboard mouse

What combination of keys produce a right-click and a left-click?