I’m writing a wrapper in Java for a C++ program. The wrapper is done using SWIG. I have the following problem: when I call a function from Java which create a big object in C++, Java doesn’t “see” that it has allocated a lot of memory since it’s not allocated in Java’s heap. The problem is that the garbage collector is not called when the object is deleted as from Java side there is plenty of free memory. What I have tried is to implement what is described here: http://www.swig.org/Doc1.3/Java.html#java_heap_allocations. The idea is to allocate memory space for C++ in Java’s heap. As I’m not interested to use that for every new, I have renamed the new and delete and use them explicitly from my C++ code where needed.
This mechanism seems to work (I can see in that Java’s heap is growing and shrinked by the garbage collector) but unfortunately I have a random crash whis seems to occur during a memcpy.
If I invoke the garbage collector manually my program is working but it’s not a very clean method.
Thanks for any clue.
In fact I didn’t notice the following line in swig website:
This seems to have solved the problem