根据 JNI_CreateJavaVM 创建 JVM,收到 OutOfMemoryError
我正在使用每个 JNI 的 C++ 程序创建一个 JVM,并且创建本身运行良好。与 JVM 的通信也工作得很好;我能够找到类、创建对象、调用方法等等。但是我的一个方法需要相当多的内存,并且JVM在调用它时抛出OutOfMemoryError。我不明白,因为有超过 1 GB 的可用 RAM。整个过程使用了大约200MB,而且似乎它甚至没有尝试分配更多;它停留在 200MB,然后抛出异常。
我尝试将-Xmx-选项传递给JVM,但是当通过JNI创建JVM时它不起作用。据我了解,通过 JNI 创建的 JVM 应该能够访问所有可用内存,从而不需要 -Xmx-options - 但显然这个假设是错误的。
所以问题是,我怎么能说 JVM 应该使用它需要的内存呢?
系统:MacOS 10.6
创建 JVM:
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
//Path to the java source code
options.optionString = jvm_options; // setting the classpath
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
I'm creating a JVM out of a C++-program per JNI, and the creation itself works fine. The communication with the JVM works also fine; I am able to find classes, create objects, call methods and so on. But one of my methods needs quite a lot of memory, and the JVM throws a OutOfMemoryError when calling it. Which I don't understand, as there is more than one GB of free RAM available. The whole process uses about 200MB and it seems that it doesn't even try to allocate more; it sticks at 200MB and then the exceptions is thrown.
I tried to pass the -Xmx-option to the JVM, but it won't work when the JVM is created through JNI. As far as I understood, a JVM created through JNI should be able to access all the memory available, making the -Xmx-options unnecessary - but obviously this assumption is wrong.
So the question is, how can I say the JVM that it just should use as much as memory as it needs?
System: MacOS 10.6
Creation of the JVM:
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
//Path to the java source code
options.optionString = jvm_options; // setting the classpath
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎我的 -Xmx-选项出了问题 - 再试一次,现在可以了。
Seems like I got something wrong with the -Xmx-option - tried it again and it works now.