gpt4 book ai didi

java - 在 JNI 中从 C++ 代码创建 NewArray 时如何释放 Array 的缓冲区?

转载 作者:行者123 更新时间:2023-11-28 01:25:02 25 4
gpt4 key购买 nike

我目前正在使用 Java 作为 GUI 使用 C++ 编写一个小型模拟器。为了实现这一点,我从我的 C++ 代码进行 JNI 调用,将数据数组传递给 GUI 应用程序。但是,由于我在测试运行中进行的调用量很大,很明显在我传递数据的函数中发生了内存泄漏。

在我的程序运行之前: Before my program has run

在我的程序运行并由于内存不足而崩溃后: enter image description here(请忽略此程序当前使用的 CPU 使用率,我知道通过 JNI 重复调用效率低下,对此我有其他解决方法)

在对发生的事情进行全面分析后,我得出结论,导致内存泄漏的不是 Java GUI 类,而是将数据数组传递给 Java GUI 的函数中的代码:


//java.env is the JNIEnv*
//setDisplay_ is a valid non-null jmethodID at runtime
//displayObject is a valid non-null jobject at runtime

void Display::setDisplay(vector<uint32_t>& a)
{
jint* buffer = new jint[a.size()];
for(int i = 0; i < a.size(); i++)
buffer[i] = (jint)a[i];
jintArray par = java.env->NewIntArray(a.size());
java.env->SetIntArrayRegion(par, 0, a.size(), buffer);
java.env->CallVoidMethod(displayObject, setDisplay_, par);
//java.env->ReleaseIntArrayElements(par, buffer, 0);
delete buffer;
}

我唯一能看到这个函数导致内存泄漏的是 jintArray我完全不知道当它超出范围时会发生什么,所以我只能假设是我释放缓冲区时的问题。但是,查看其他人使用 JNI 和数组的示例代码(例如:here),我注意到他们从不释放他们创建的数组。在挖掘 JNI Documentation 时我遇到了 Release<NativeType>ArrayElements根据描述,我假设的方法是我正在寻找的方法:

ReleaseArrayElements Routines void ReleaseArrayElements(JNIEnv *env, ArrayType array, NativeType *elems, jint mode); A family of functions that informs the VM that the native code no longer needs access to elems. The elems argument is a pointer derived from array using the corresponding GetArrayElements() function. If necessary, this function copies back all changes made to elems to the original array. The mode argument provides information on how the array buffer should be released. mode has no effect if elems is not a copy of the elements in array. Otherwise, mode has the following impact, as shown in the following table:

真正让我希望这是我特别需要的是

The mode argument provides information on how the array buffer should be released

然而,经过进一步检查,我不太确定这是我原先认为的方法,并且这在测试中证明了自己,而且它似乎调用了 exit()。一旦发生故障(因为 JNI 如此臭名昭著)并且每次我使用文档中提供的任何模式运行它时都会发生这种故障。

所以我真正的问题是:创建时 New<PrimitiveType>Array从 JNI 中的 C++ 代码如何释放 <PrimitiveType>Array的缓冲区?

最佳答案

经过更多挖掘,我发现了 Do I need to call ReleaseIntArrayElements on an array created with NewIntArray? @gerbit 的简短回答:

You have to release only reference:

jintArray pixels = env->NewIntArray(width * height);
env->DeleteLocalRef(pixels)

显然,当在 Java 调用 C++ 的方向上使用 JNI 时,您不需要清理 <PrimitiveType>Array是因为 java 会为您处理这个。但是从C++方向调用Java时,需要调用DeleteLocalRef()为了防止内存泄漏。

关于java - 在 JNI 中从 C++ 代码创建 New<PrimitiveType>Array 时如何释放 <PrimitiveType>Array 的缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com