Reputation: 79
I have a C program loading the libjvm dynamically with dlopen(), finding the JNI_CreateJavaVM function address with dlsym() and initializing the JVM with it.
After doing some Java stuff (that already works fine), I would like to release the JVM resources and make the process reuse the memory that was (m)allocated by the JVM on the heap...
I am trying (*jvm)->DestroyJavaVM(), then allocating more memory, but it does not seem to reuse what was allocated by the JVM.
Is there some other API to be called to force the JVM to free the allocated memory?
I mean, in a regular usage, you don't care to release the resources at the end of a program, because the OS will release the memory used by the process. But here I want to force a cleanup to reuse resources for another library consuming a lot of memory.
Does that make sense by the way or do I miss something?
Thanks! Seb
Upvotes: 0
Views: 273
Reputation: 98505
HotSpot JVM does not ever attempt to release all the resources at exit. In particular, it does not deallocate Object Heap, Code Cache, Metaspace and so on. This memory cannot be reclaimed until the process terminates.
The VM shutdown sequence is described in the comments to Threads::destroy_vm
. Note that it does not say anything about releasing memory. Also, JVM does not attempt to kill or suspend daemon threads.
The documentation to DestroyJavaVM
says that
Unloading of the VM is not supported.
A typical solution is to launch JVM in a separate process.
Upvotes: 2