search

Home  >  Q&A  >  body text

java - c++ Creating jvm through jni is much more stuck than starting jar directly?

I want to use jni to call the entry main function of the java program in c++, but the jvm created in this way is much more stuck than running it directly with the javaw command, but the jvm parameter settings are the same. When running in c++, the fps is only 10-30, but when running the jar directly, it can reach 50-100.
What is the reason for this? Are there any ways to improve this?

#include <jni.h>
using namespace std;
extern "C" {
    __declspec(dllexport) DWORD NvOptimusEnablement = 1;
}
int main() {
    JavaVMOption options[7];
    JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jclass cls;
    jmethodID mid;

    options[0].optionString = "-XX:+UseG1GC";
    options[1].optionString = "-XX:-UseAdaptiveSizePolicy";
    options[2].optionString = "-XX:-OmitStackTraceInFastThrow";
    options[3].optionString = "-Xmn512m";
    options[4].optionString = "-Xmx2048m";
    options[5].optionString = "-Djava.library.path=natives";
    options[6].optionString = "-Djava.class.path=.;libraries.zip;main.jar";

    std::memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 7;
    vm_args.options = options;
    cout << "start creat jvm!\n";
    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if (status != JNI_ERR) {
        cout << "jvm created success\n";
        cls = env->FindClass("main/Main");
        if (cls != 0) {
            mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
            if (mid != 0) {
                jclass clz_string = env->FindClass("java/lang/String");
                if (clz_string != NULL) {
                    jobjectArray result = env->NewObjectArray(2, clz_string, NULL);
                    jstring arg0 = env->NewStringUTF("--accessToken");
                    jstring arg1 = env->NewStringUTF("14233482b8dbad97617757a5c31d5872");
                    env->SetObjectArrayElement(result, 0, arg0);
                    env->SetObjectArrayElement(result, 1, arg1);
                    env->CallStaticVoidMethod(cls, mid, result);
                }
            }
        } else {
            cout << "class not found\n";
        }
        jvm->DestroyJavaVM();
        return 0;
    } else {
        return -1;
    }
}
巴扎黑巴扎黑2738 days ago780

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:23:53

    You will know if you have seen the source code of javaw.exe in the jdk source code. It is also implemented using JNI_CreateJavaVM, so you can compare the difference between your code and javaw.exe.

    Another two possibilities come to mind:

    1. The jvm under Windows has serverclient两种类型,性能差别还是挺大的,javaw.exe会自动选择(也可以用-server-clientspecified). When calling with c++, it seems that you need to specify which jvm.dll to use;

    2. Which standard library (single-threaded or multi-threaded) is used when compiling c++? It is unclear whether it will affect the calling of jvm.

    reply
    0
  • Cancelreply