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?

Germán Diago
  • 7,473
  • 1
  • 36
  • 59
  • 2
    Why don't you do it in C++ instead? – Danh Nov 29 '16 at 08: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. – Germán Diago Nov 29 '16 at 08:40
  • Is there a reason you can't maintain a reference to your `byte[]` in `Java` to prevent the Garbage Collector from claiming it? – Galik Nov 29 '16 at 08:59
  • Yes, forget the GC. It's not your problem here. – Kayaman Nov 29 '16 at 09:01
  • 1
    See related questions: http://stackoverflow.com/questions/1491519/any-concept-of-shared-memory-in-java, http://stackoverflow.com/questions/15130283/java-and-c-shared-memory, http://stackoverflow.com/questions/15414753/shared-memory-between-c-and-java-processes, http://stackoverflow.com/questions/904492/good-alternative-to-shared-memory-ipc-for-java-c-apps-on-linux – Kayaman Nov 29 '16 at 09:02
  • MappedByteBuffer seems to do the job. – Germán Diago Nov 29 '16 at 09:14
  • Or a direct ByteBuffer will let you access native memory in Java. – Peter Lawrey Nov 29 '16 at 09:16
  • I cannot figure out yet how a ByteBuffer can be used. You directAllocate memory, that is ok. But how do I pass where to get the memory from to Java? – Germán Diago Nov 29 '16 at 09:25
  • 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. – Wilfredo Pomier Nov 29 '16 at 11:37
  • They run in the same process. But I fail to see how that would avoid copying the data. What I want is to take a C++ buffer and get a reference seen as a byte[] in Java. Without copying and without Java ever releasing it. – Germán Diago Nov 29 '16 at 15:53
  • When you allocate the array in C++, it is possible to just allocate it as a Java byte buffer or byte array in the first place before you fill it with your data on the C++ side? Then you won't ever need to do a copy. – samgak Dec 01 '16 at 02:42
  • @samgak unfortunately this is an already existing C++ buffer :( I cannot be intrusive to that part. My only choice is to reference it I think. – Germán Diago Dec 01 '16 at 07:16

0 Answers0