1

I have an application with high perf. requirements.

The problem at hand is that I need to invoke some Java API to copy a native (C++) array into disk, from a Java API. The data is going to be big, so I want to avoid copies from C to Java land. I want just to pass a reference to the data in some way and serialize that data

The problem is that if I send a byte[] the Java GC will delete it and we do not want this to happen.

I thought of wrapping my native array data in a class/struct in C++ but at the end I need a byte[] stream or similar in the Java side. Is there a way I can cast some reference to some data in C++ to byte[] in Java, and immediately after that leave that memory alone, avoiding the GC to recycle it?

asked Nov 29, 2016 at 8:32
12
  • 2
    Why don't you do it in C++ instead? Commented Nov 29, 2016 at 8:37
  • Because I need to call an already implemented Java API that provides much more functionality. It does not only save things, it adds it into a database and does more stuff. Commented Nov 29, 2016 at 8:40
  • Is there a reason you can't maintain a reference to your byte[] in Java to prevent the Garbage Collector from claiming it? Commented Nov 29, 2016 at 8:59
  • 1
  • 1
    If both java and c++ code are running inside the same process, and if you can modify the java source then you can create a method in java to allocate and return a memory chunck, fill it in C++ and call another java method to process it in java. C++ is far more flexible than java. Now, if they aren't in the same process, then using Shared Memory or Memory-mapped files can be your solution. Commented Nov 29, 2016 at 11:37

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.