JNI DeleteLocalRef澄清 [英] JNI DeleteLocalRef Clarification

查看:88
本文介绍了JNI DeleteLocalRef澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:

jstring jstrKey;
for(int i=1;i<=1000;++i) {
    LPWSTR strKey = L"string";
    jstrKey = env->NewString((jchar *)strKey, wcslen(strKey));
}
env->DeleteLocalRef(jstrKey);

问题2:

for(int i=1;i<=1000++i) {
    LPWSTR strKey = L"string";
    jstring jstrKey = env->NewString((jchar *)strKey, wcslen(strKey));
    env->DeleteLocalRef(jstrKey);
}

在两个问题中我都正确使用DeleteLocalRef吗?

Am i using DeleteLocalRef properly in both questions?

尤其是在问题1中,我在循环后删除了本地引用.我认为这是正确的,并且由于不需要创建任何新的本地引用,因此不必在循环内调用deletelocalref.

Especially in Question 1, I am deleting local ref after the loop. I think that is correct, and need not call deletelocalref inside the loop since I am not creating any new local ref.

那么在使用DeleteLocalRef方面没有问题吗?

So no issues with respect to usage of DeleteLocalRef right?

推荐答案

在这两种情况下,都应在循环内调用DeleteLocalRef(),因为每个NewString()都创建一个新的本地引用.

In both cases you should call DeleteLocalRef() inside the loop because each NewString() crerates a new local ref.

在从本机方法返回时,JNI将丢弃本地引用,但是此过程与Java垃圾回收无关.通常,我们无需担心本地引用.但是本地引用表通常很小,因此我们必须丢弃在相当长的循环中创建的未使用的引用.

Local references will be discarded by JNI on return from a native method, but this process has nothing to do with Java garbage collection. Usually, we don't need to worry about local references. But the local ref table is usually quite small, therefore we must discard unused references which are created on a significantly long loop.

这篇关于JNI DeleteLocalRef澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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