Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Does DestroyJavaVM() [i]really[/i] destroy the JVM?

843829Feb 8 2005 — edited Nov 17 2007
Hi,

If anybody has any advice on this issue I would really appreciate it. I use Java in my C++ application through JNI. For various reasons, once I'm done making my Java calls, I no longer want the JVM to be running. The trouble I'm having is that even after making the call to DestroyJavaVM(), the JVM seems to continue to run, as evidenced by the fact that if my code subsequently does something like throw a signal, the JVM picks up on it and spits out an error message. Here is a simple program that illustrates my point:
#include <iostream>
#include <jni.h>
#include <string>

using std::cerr;
using std::endl;
using std::string;

static JNIEnv* envHandle;
static JavaVM *jvm;

const string DISABLE_JIT_OPTIONS = "-Djava.compiler=NONE";
const string USER_CLASSES_OPTIONS = "-Djava.class.path=.:";

int main(int argc, char **argv)
{
  // Initialize a JVM
  JavaVMOption options[4];
  JavaVMInitArgs vm_args;
  jint jstatus;

  options[0].optionString = const_cast<char*>(DISABLE_JIT_OPTIONS.c_str());

  string userClassesOptions = USER_CLASSES_OPTIONS;
  string minHeapSize = "-Xms128m";
  string maxHeapSize = "-Xmx1024m";

  const char* javaLibs = getenv("JAVA_LIB_HOME");
  userClassesOptions += javaLibs;
  options[1].optionString = const_cast<char*>(userClassesOptions.c_str());
  options[2].optionString = const_cast<char*>(minHeapSize.c_str());
  options[3].optionString = const_cast<char*>(maxHeapSize.c_str());

  vm_args.version = JNI_VERSION_1_2;
  vm_args.options = options;
  vm_args.nOptions = 4;
  vm_args.ignoreUnrecognized = JNI_FALSE;

  jstatus = JNI_CreateJavaVM(&jvm, (void**)&envHandle, &vm_args);
  if (envHandle->ExceptionOccurred())
  {
    cerr << "Unable to initialize VM" << endl;
    exit(-1);
  }

  // Destroy the JVM
  jvm->DestroyJavaVM();
  if (envHandle->ExceptionOccurred())
  {
    cerr << "Unable to destroy VM" << endl;
    exit(-1);
  }

  // Do something stupid...
  int *a = 0;
  int b = *a;
}
So, if you look at the 'Do something stupid' piece of code, that generates a seg fault, which I would like to handle myself, but it looks like the JVM picks up on even though I previously destroyed it and spits out the following message:

****************
Another exception has been detected while we were handling last error.
Dumping information about last error:
ERROR REPORT FILE = (N/A)
PC = 0x0x8049def
SIGNAL = 11
FUNCTION NAME = (N/A)
LIBRARY NAME = (N/A)
Please check ERROR REPORT FILE for further information, if there is any.
Good bye.
Abort

Is there something wrong in the way I'm destroying the JVM? Does anybody know why this happens?

Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2007
Added on Feb 8 2005
5 comments
1,671 views