Geek
Geek

Reputation: 23409

How is native code handled by the JVM

Consider an case that I have to call C++ code from my Java Program. The C++ code creates thousands of Objects. Where are these dynamic objects stored ? I suspect in the JVM heap because the native code will be a part of the same process as the JVM.

If yes, do the rules of Java Garbage collector thread apply on Objects of the C++ code ?

Upvotes: 5

Views: 1105

Answers (1)

ConcernedOfTunbridgeWells
ConcernedOfTunbridgeWells

Reputation: 66612

For the first question, C++ will allocate resources using its own runtime which has nothing to do with the JVM - the JVM is not aware of any activity in this memory allocator.

For the second question, the Java garbage collector will not GC the memory allocated by C++. You will have to make sure that your Java wrapper initiates the memory release. Before an object is GC'd by java, the runtime calls the finalize() method. The default one is inherited from java.lang.Object and basically does nothing. You can override this and use it as a hook to initiate deallocating your manually managed memory.

Upvotes: 5

Related Questions