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

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!

You will need to wait for any thread(s) to finish and exit before the VM will shut down, and even then it may not be destroyed, depending on the JVM version.
Take a look at this reference:
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/intro.html
    In JDK 1.1.1, the thread calling DestroyJavaVM must be the only user
    thread in the VM. JDK 1.1.2 has lifted this restriction. If DestroyJavaVM
    is called when there is more than one user thread, the VM waits until
    the current thread is the only user thread, and then tries to destroy itself.The spec does not promise the VM will actually be unloaded, so this part may depend on the implementation:
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html#destroy_java_vm

Similar Messages

  • Does two java enabled browser share the JVM

    Hello all,
    do two or more java enabled browser windows running share the JVM.

    Hai Kai,
    so it means that I have to download the java plugin from java.sun.com?
    can you help me by giving the exact address
    sorry to trouble you
    thanks

  • Does This Software Really Can Bootup The Sound Quality?

    hi,
    please check out the software at the following link:
    http://www.fxsound.com/mp3-accessories-store/ipod-accessories/ipod-accessories-i dfx-plus.html?subvendor=309
    it's called iDFX Plus.
    do you think it really can work?
    BTW, IS .AAC BETTER OR .MP3 BETTER FOR ME TO RIP MY CDS
    want to use 320, but afraid ipod doesn't have enough space for all my songs.
    192 .mp3? or 192 .aac?
    or other suggestions?
    thanks.
    j

    I think that this KB article does a fair job at talking about the encoding methods and comparisions:
    http://support.apple.com/kb/HT2103
    As according to apple a 128 AAC is equal if not greater than a 160 MP3. However AACs are restricted formats so they can't be used on some other media devices... So on the flip if you want universal MP3 product support MP3 is the suggested way on that topic.
    Good Luck and let us know what encoding method that you decide to use!

  • JNI_CreateJavaVM does not load the JVM

    I'm trying to load the JVM from my C++ code in order to invoke existing Java API. Here is the code that I'm using to do it.
    #define JAVAHOME "C:\\jdk1.5.0_03" /* Java 1.5.0 install directory */
    #define HOME_DIR "M:"
    #define ADMINAPI_HOME HOME_DIR "\\obj\\w32ud\\apps\\shared\\3rdparty\\admin\\lib"
    #define DEFAULT_CLASSES "."
    #define JAVA_CLASSES JAVAHOME "\\jre\\lib\\rt.jar" /* Standard Java Classes jar */
    #define ADMIN_CLASSES ADMINAPI_HOME "\\admin.jar"
    #define JAVA_WRAPPER HOME_DIR "\\classes\\AdminWrapper.jar"
    this->m_VMArgs.version = JNI_VERSION_1_4;
    this->m_VMArgs.nOptions = 3;
    this->m_JVMOptions[0].optionString = "-Djava.class.path=" DEFAULT_CLASSES ";" JAVA_CLASSES ";" ADMIN_CLASSES ";" JAVA_WRAPPER;
    this->m_JVMOptions[1].optionString = "-Djava.compiler=NONE"; /* disable JIT */
    this->m_JVMOptions[2].optionString = "-Djava.home=" JAVAHOME; /* Specify Java Home directory location */
    this->m_VMArgs.options = m_JVMOptions;
    this->m_VMArgs.ignoreUnrecognized = JNI_FALSE;
    JNI_CreateJavaVM (&m_pJVM, (void **)&m_pEnv, &m_VMArgs);
    However, upon execution of the above server code, both m_pJVM as well as m_pEnv are NULL. What is the reason? I had this code (with directory path changes) work on a local machine but when I port it to the server (where M: is a mapped drive). I'm curious to know how does JNI_CreateJavaVM discovers the JVM and loads it?
    Help is appreciated.
    Thanks,
    Suhas.

    You will never get that to run if you use hardcoded values.
    As you are on win try to query the registry :
    HKEY_LOCAL_MACHINE,"SOFTWARE\\JavaSoft\\Java Runtime Environment"
    and query current version and installation path.
    btw: I hope you did not really place your jars at the root (/) of you server... :)

  • Why does using a static jclass variable within a method crash the JVM?

    I have some native code, that in a single thread, loops over a Java method. To improve performance, I tried this:
        static jclass clazz = env->FindClass("foo");
        /*FIXME: GetStaticMethodID crashes with "Bad global or local reference"*/
        jmethodID mid = env->GetStaticMethodID(clazz, "makeEvent", sig);But with the static declaration of clazz, the JVM crashes on the second iteration. Why? Is this really any different then using a C++ member field ?
    My loop is really a bit more complicated then this, in that it starts several calls up. I hate the idea of
    A) actually calling FindClass every iteration.
    B) passing the clazz pointer through 3 layers of calls, just to avoid calling FindClass
    Is there a better way?
    Thanks!

    Well I tried this, and it does not compile, becuase NewGlobalRef returns jobject not jclass.
    Yes "jclass" is a pointer to a c++ object jclass, and jclass extends _object. However,
    static jclass myGlobalRef dynamic_cast<jclass>( NewGlobalRef (myJClass));Does not compile either. The error is
    error C2683: 'dynamic_cast' : '_jobject' is not a polymorphic type
    static jclass myGlobalRef static_cast<jclass>( NewGlobalRef (myJClass));
    DOES compile, but it is very dangerous. And using static_cast when dynamic_cast fails is a super-bad idea.
    Just for laughs, I tried it anyway, and when I use -Xcheck:jni, the JVM dies with a fatal error.
    Edited by: charlweed on Sep 19, 2009 3:56 PM
    Typo

  • My MacBook Pro is really hot and the fan is on constantly. It didn't use to do this, does anyone know why this is happening? I have a very short battery life, so I have to keep it plugged in probably more than I should.

    My MacBook Pro is really hot and the fan is on constantly. It didn't use to do this, does anyone know why this is happening? I have a very short battery life, so I have to keep it plugged in probably more than I should.

    Open Activity Monitor in the Utilities folder.  Select All Processes from the Processes dropdown menu.  Click twice on the CPU% column header to display in descending order.  If you find a process using a large amount of CPU time, then select the process and click on the Quit icon in the toolbar.  Click on the Force Quit button to kill the process.  See if that helps.  Be sure to note the name of the runaway process so you can track down the cause of the problem.

  • How does the JVM call Windows GDI Text functions?

    I need to find out how the JVM calls Windows GDI functions for Java running on Windows. I am a programmer at an imaging software company and we "hook" to client applications to extract information for searching and indexing. Basically what we do is tell the app to refresh itself and then we intercept the text output functions to the GDI. Recently, when trying to image enable an Oracle app (Sun JVM or bastard version thereof), we failed to capture any text output. None of the documented MS GDI text output functions are being called to display that text. Does anyone with an intimate knowledge of the workings of the JVM on a Windows platform know how this is accomplished. I currently have calls pending at Sun and Microsoft, but sometimes these forums have more info than they do. Thanks in advance for any help you can provide. ([email protected])
    Jason Nix

    I would venture to say that SUN is generating its own
    text. That would be necessary in order to handle all
    of the sizing and layout management issues which
    happen in Java real time, as opposed to Windows once
    at compile time.Not likely. Font rendering is always done at runtime and it would be generally unwise to do it yourself given the availability of TrueType fonts and the highly optimized rendering engines. I would be shocked if Sun tried to do it themselves.
    Chuck

  • When using quick edit for compressed CSS, edge code does not jump to the particular class but displays the whole thing. This destroys the whole purpose. Does anyone know a fix or extension for that.

    When using quick edit for compressed CSS, edge code does not jump to the particular class but displays the whole thing. This destroys the whole purpose. Does anyone know a fix or extension for that.

    I suspect that beautify doesn't touch CSS that it is not aware  of (e.g. new properties/syntaxes, vendor-prefixes). See if there's a newer version available.
    Which compressed CSS are you using? Most CSS libraries come with both compressed and original stylesheets, so use the original one during development/debugging, and switch to the compressed one for deployment.
    Randy

  • In which language does the jvm program is written?

    In which language does the jvm program is written?
    Please give the reason for that
    Thanks,
    Vidyadhar

    Dear Nisha,
    you can use table GRACREVREJUSER with the correpsonding reasons from table GRACREJREASON and GRACREJREASONT.
    Regards,
    Alessandro

  • How to solve: The JVM shared library "/System/Library/Frameworks/JavaVM.framework" does not contain the JNI_CreateJavaVM symbol ?

    I cannot run Eclipse or Aptana any more because I get the following error
    The JVM shared library "/System/Library/Frameworks/JavaVM.framework"
    does not contain the JNI_CreateJavaVM symbol.
    I already tired to reinstall Java from the oracle website but it does not help.

    By everything, did you mean eclipse and java?

  • Does the jvm allocate complete max heap size initially?

    Does the JVM allocate the memory for the entire max heap size up front, or does it start with the specified minimum size and increase and grab more later if needed?
    The reason this is being posted is because we have a number of jboss servers that are being run. Some don't require large heap sizes, others do. If we use the same large max heap size for all would all the memory get allocated up front, or possibly a smaller initialization portion?

    I have done the test with Solaris, Linux and WinXP.
    Test with -Xms512M
    Have written a simple java program to which the minimum heap size was set to -Xms512m then the program was executed on Solaris and WinXP platforms. The usage of memory of the Java process was 6 MB in WinXP and 9 MB in Solaris, rather than 512 MB. The JVM is not allocating the configured minimum size of 512 MB at the start of the process execution.
    Reason:
    If you ask the OS for 512 MB it'll say "here it is", but pages won't actually be allocated until your app actually touches them.
    If the allocation is not being made initially during the start of the process, the concept of minimum heap size is not required.
    But the garbage collection log shows the minimum heap size as what was configured using -Xms option.
    Test with -Xms1024M
    The JVM arguments was set to : -Xms1024m -Xmx1024m, but the used memory observed using Windows perfmon was 573M.
    6.524: [Full GC 6.524: [Tenured: 3081K->10565K(967936K), 0.1949291 secs] 52479K->10565K(1040512K), [Perm : 12287K->12287K(12288K)], 0.1950893 secs] Reason:
    Optimization is something that the operating systems do. The JVM allocates the memory in it's address space and initializes all data structures to your -Xms. In any way that the JVM can measure, the allocation from the OS is complete. But the OS doesn't physically assign a page to the app until the first store instruction. Almost all modern OSs do this.
    Hope this is helpful.

  • When does the JVM exit and deleteOnExit();?

    I developed a JSP that queries a database, displays the results in a browser, and writes the results to the server as a file. While the user is viewing the JSP, they can click on a download link to save the file that was written to the server. Everything works fine!
    I would like to delete the file that was written to the server (to save space), however, I can't delete the file until the user is finished viewing the JSP. I need some help concerning this method: java.io.File.deleteOnExit() the API states:
    "Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification (12.9)."
    If I use this method, When does the JVM exit? I tested it in my home environment using Tomcat and the file was deleted when I stopped Tomcat. If I run this on a web server, does the JVM ever exit? If so when?
    Thanks
    --Scott

    The JVM stays active so long as the Java based webserver (or the servlet engine if your webserver is connected to one) is up. Only when you shutdown your server will the VM terminate. (Similar to what you observed with tomcat)

  • How does the JVM handle private class parameters in a utility class?

    If I have a class that is public and not labeled as static, but it's constructor is private so that it cannot be instantiated, and all of its parameters and methods are both private and static. The methods modify the private parameters. In this case, the code explicitly is avoiding a singleton pattern, so there isn't even a class being instantiated, so this is a true utility class.
    However, the static methods are accessible from multiple objects, and as far as I can tell, the JVM is persisting the parameters in memory beyond the specific thread that called the static method to begin with.
    The question I have is how the JVM handles this? Are these private parameters assigned memory and are indeed persisted independent of thread, since there is no class reference. I am working with byte code injection, and the assumption is that a direct reference to a static parameter is much faster to call than having to go through an object reference. Of course, from a programmer point of view, I much prefer the singleton pattern since I am so used to hang my hat on a class reference.
    Thanks.

    entguru wrote:
    However, the static methods are accessible from multiple objects...Not if the methods are private, as you seemed to indicate ("all of its parameters and methods are both private and static").
    Are these private parameters assigned memory and are indeed persisted independent of thread, since there is no class reference. There is a class reference. It refers to the class, and is obtainable via the class literal; e.g., MyClass.class.
    ~

  • Does the JVM ever return memory back to the OS?

    Hello Gurus,
    I have an OC4J application running on Solaris that have some memory spikes (the jvm is inovked with -xms256m -xmx1024m).
    I've noted that once the heap expands, even if it shrinks back, the jvm process keeps on using the bigger memory footprint (in other words, maybe the jvm indeed manages a smaller heap size but if I execute the "top" command from a terminal - I never see that memory actually being freed).
    For the math people: my jvm memory consumption is a non-decreasing monotonic function.
    Is that a bug or the designed behavior?
    Thanks in advance!
    Amnon Sadeh.

    The physical memory and swap reservation is indeed returned
    to the OS if it's not in use. The virtual space reservation, however,
    stays.. The JVM is however circumspect/inertial is reacting to
    temporary fluctuations in java heap footprint, and employs
    a hysteretic low-pass-filter to protect against what might be
    small, high frequency fluctuations in the program's heap
    footprint.
    Hello Gurus,
    I have an OC4J application running on Solaris that
    have some memory spikes (the jvm is inovked with
    -xms256m -xmx1024m).
    I've noted that once the heap expands, even if it
    shrinks back, the jvm process keeps on using the
    bigger memory footprint (in other words, maybe the
    jvm indeed manages a smaller heap size but if I
    execute the "top" command from a terminal - I never
    see that memory actually being freed).
    For the math people: my jvm memory consumption is a
    non-decreasing monotonic function.
    Is that a bug or the designed behavior?
    Thanks in advance!
    Amnon Sadeh.

  • How does the JVM recover from a java.lang.StackOverflowError?

    As far as I know that whenever a java.lang.OutOfMemoryError is thrown, the application is in an unknown state and only a restart of the JVM can fix this. How about java.lang.StackOverflowError? If uncaught, the calling thread is terminated for sure, but the other threads? Are there side-effects?

    In windows OS that thread will die and thats all.
    I think that in some unix systems the whole process
    is likely to fail.Not exactly. It's not nearly as dependent on the operating system as it is on the quality of the JVM implementation. StackOverflowErrors are tricky to handle correctly in all cases, but most JVMs (including Sun's) now properly handle the vast majority of them. FWIW, most unix-based or unix-like operating systems make it somewhat easier to deal with StackOverflowError than Windows, particularly Windows versions prior to Win2K.
    A thread that triggers a StackOverflowError can actually catch it and recover; the important thing to note is that if you want to keep executing code on that thread, you have to allow many or most of the activations (i.e., function calls) on the stack to be unwound. Otherwise, you'll quickly provoke another StackOverflowError since you will still be close to the end of the stack.

Maybe you are looking for