Java JNI 错误:本地引用表溢出 512 个条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20917777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JNI error : Local reference table overflow 512 entries
提问by rvp
My function looks like below. And it is getting executed for a number of times.At certain point it is crashing at jobject nvarObject = env->GetObjectField (var1, nvar1) giving error JNI error : Local reference table overflow 512 entries.
我的功能如下所示。并且它被执行了多次。在某些时候它在 jobject nvarObject = env->GetObjectField (var1, nvar1) 上崩溃,给出错误 JNI 错误:本地引用表溢出 512 个条目。
Could anyone look into this issue and throw some light.
任何人都可以研究这个问题并提出一些意见。
采纳答案by Jason LeBrun
All JNI methods that return a jobject
or similar object reference are creating local references in the reference table. These references get automatically cleaned up when you return control to the JVM, but if you are creating many references (for instance, in a loop), you'll need to clean up them up manually.
所有返回一个jobject
或类似对象引用的JNI 方法都在引用表中创建本地引用。当您将控制权返回给 JVM 时,这些引用会自动清除,但是如果您要创建许多引用(例如,在循环中),则需要手动清除它们。
You're on the right track by calling DeleteLocalRef
on the cls
reference, but notice that GetObjectField
also returns a jobject
, so the reference returned there should be deleted before exiting the function, as well.
您通过调用引用走在正确的轨道DeleteLocalRef
上cls
,但请注意,它GetObjectField
也返回 a jobject
,因此在退出函数之前也应删除在那里返回的引用。
Also make sure to clean up any existing references before returning from error conditions!
还要确保在从错误条件返回之前清理任何现有的引用!
Another way to do this: at the top of the function that you're calling in a loop, call PushLocalFrame( env, 5 )
and call PopLocalFrame(env)
before any place in the function where you return. This will automatically clean up any references created during that function call. The second argument is the number of local references you want in the frame -- if you need more than 5 local references during the execution of the function, use a value higher than 5.
另一种方法是:在循环中调用的函数的顶部,在函数中返回的任何位置之前调用PushLocalFrame( env, 5 )
和调用PopLocalFrame(env)
。这将自动清除在该函数调用期间创建的任何引用。第二个参数是你想要在框架中的本地引用的数量——如果在函数执行期间你需要超过 5 个本地引用,请使用大于 5 的值。