垃圾收集和JNI调用 [英] Garbage collection and JNI call

查看:131
本文介绍了垃圾收集和JNI调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了JNI程序随机内存不足的问题。

I am having an issue with a JNI program randomly running out of memory.

这是一个32位的java程序,它读取文件,进行一些图像处理,通常使用250MB到1GB。然后丢弃所有这些对象,然后程序对JNI程序进行一系列调用,通常需要100-250MB。

This is a 32 bit java program which reads a file, does some image processing, typically using 250MB up to 1GB. All those objects are then discarded, and then the program makes a series of calls to a JNI program that typically needs 100-250MB.

当以交互方式运行时,我从未见过一个问题。但是,当运行批处理操作在连续的许多文件上执行此操作时,JNI程序将随机耗尽内存。它可能有一个或两个文件的内存问题,然后对接下来的10个文件运行正常,然后再次出现故障。

When run interactively, I have never seen a problem. However, when running a batch operation that does this on many files in succession, the JNI program will randomly run out of memory. It may have a memory problem for one or two files, and then runs fine for the next 10 files, and then glitch again.

我已经转储了可用内存量就在JNI呼叫之前,它遍布地图,有时是100MB,有时是800MB。我的解释是Java垃圾收集有时会在图像处理后立即运行,有时则不会。如果不是,那么JNI程序可能没有足够的内存。

I have dumped the amount of free memory right before the JNI calls and it is all over the map, sometimes 100MB, sometimes 800MB. My interpretation is that Java garbage collection sometimes is run immediately after the image processing, and sometimes not. When it is not, then there may not be enough memory for the JNI program.

我已经阅读了关于GC不确定性的所有内容,不应该调用它,不会有任何区别等等,但在启动JNI调用之前确实似乎强制GC会改善这种情况。

I have read all the stuff about GC being non deterministic, shouldn't call it, won't make any difference, etc. but it sure seems like forcing GC before starting the JNI calls would improve this situation.

但有没有什么办法可以确保在继续之前有一定数量的可用内存?

But is there any way to really ensure that there is a certain amount of free memory before continuing?

要回答有关JNI程序的问题,这是由另一家公司提供的,我对如何分配内存没有真正的了解。我所知道的是它是在c ++中,它没有垃圾收集。而且我被告知它需要100-250MB的内存,我看到的数字会证实这一点。

To answer the questions about the JNI program, that is supplied by another company, and I have no real insight into how it allocates memory. All I know is that it is in c++, which has no garbage collection. And I have been told that it needs 100-250MB of memory, and the numbers I have seen would confirm that.

也许我应该重新提出问题:如果我我即将进行一次JNI调用,我知道它需要250MB的内存,我怎么能保证它有足够的内存?

Maybe I should reword the question to be: If I am about to make a JNI call that I know will need 250MB of memory, how can I assure that it will have that much memory available?

而且肯定是一个可能的解决方案是进行64位构建。但是,这个批处理操作是32位构建的QA的一部分,所以我想测试真实的东西。

And it certainly true that one possible solution would be to do a 64 bit build. However, this batch operation is part of QA on a 32 bit build, so I would like to be testing the real thing.

推荐答案

我自己解决这个问题的方法只是调用 System.gc(),但是从里面调用本机代码:

My own approach to this problem is simply to call System.gc(), but from inside the native code:

#include <jni.h>
// ...
int my_native_function(JNIEnv* env, jobject obj) {
    jclass    systemClass    = nullptr;
    jmethodID systemGCMethod = nullptr;
    // ...
    // Take out the trash.
    systemClass    = env->FindClass("java/lang/System");
    systemGCMethod = env->GetStaticMethodID(systemClass, "gc", "()V");
    env->CallStaticVoidMethod(systemClass, systemGCMethod);
}

我希望这也适合你。

这篇关于垃圾收集和JNI调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆