Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Allocating native memory on Java heap?

nitroamosJan 30 2017

I'm trying to implement the approach described here:

SWIG and Java

which was initially described here:

https://www.usenix.org/legacy/event/usenix07/posters/sorensen.pdf

because on some machines I'm getting out of memory errors related to native memory, even though the heap seems to have plenty of memory.

This solution is intended to allow me to allocate memory in my C++ code on the Java heap. Is this an approach that should work? I tried implementing it, but I keep getting segfaults. Here's an excerpt of the method, which is inside a C++ new operator. The idea is that the new caller gets a pointer to a Java byte array which is returned to the JVM in the delete operator.

  JNIEnv *env = JNU_GetEnv();

  jbyteArray jba = env->NewByteArray((int) t + sizeof(Jalloc));

  void *jbuffer = static_cast<void *>(env->GetByteArrayElements(jba, 0));

  Jalloc *pJalloc = static_cast<Jalloc *>(jbuffer);

  pJalloc->jba = jba;

  pJalloc->ref = env->NewGlobalRef(jba);

  return static_cast<void *>(static_cast<char *>(jbuffer) + sizeof(Jalloc));

Thanks!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 27 2017
Added on Jan 30 2017
0 comments
1,029 views