根据 JNI_CreateJavaVM 创建 JVM,收到 OutOfMemoryError

发布于 2024-10-26 17:44:44 字数 849 浏览 0 评论 0原文

我正在使用每个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-11-02 17:44:44

似乎我的 -Xmx-选项出了问题 - 再试一次,现在可以了。

Seems like I got something wrong with the -Xmx-option - tried it again and it works now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文