操作 256x256 位图导致 android-ndk 的 JNI 崩溃
也许我正在尝试做一些我不应该做的事情。
我正在模拟器中运行一段代码。看起来(或多或少) 像这样: http://pastie.org/1291380
这是构建一个动态壁纸背景。我传入一个位图, 调色板和图块数组。我的位图的大小是 256 x 256。 getRedPal / getGreenPal / getBluePal 本质上是调用 Color.red() / Color.green() / Color.blue() 以获得rgb 调色板对象的颜色组件。
循环嘎嘎作响;我一直到 j 值 在模拟器崩溃和烧毁之前,drawInC 的数量达到 32:
11-11 15:34:44.032: INFO/ Distre_bmp(598): DrawInC: i: 0 j: 32
11-11 15:34:44.032: INFO/ Distre_bmp(598 ):DrawTiles:i:0 j:0
11-11 15:34:44.032:INFO /您扭曲_bmp(598):DrawTiles:i:0 j:1
11-11 15:34:44.032:INFO /您扭曲_bmp(598): DrawTiles:i:0 j:2
11-11 15:34:44.032:INFO /您扭曲
_bmp(598):DrawTiles:i:0 j:3 11-11 15:34:44.032:INFO /您扭曲_bmp(598):DrawTiles: i: 0 j: 4
之后,我将转储文件发送到 /data/tombstones 。这是 转储(但我真诚地发现其中没有任何价值,只是 一堆内存地址): http://pastie.org/1291394
我添加了 android:vmSafeMode="true"之后到我的标签 在其他地方读到这可以解决问题。这是2.2上的, 使用位图.h。
就我个人而言,我对这个
jbyte* buffer =
(*env)->GetByteArrayElements(env, arr, &isCopy)
呼吁持怀疑态度。我摘下那个 来自网络某处的代码,因为我完全无法获取值 来自我的字节数组“arr”。
有什么想法吗?
编辑 在操作我的循环迭代器(我缩短了循环次数)之后,我现在收到一个信息性错误:
“ReferenceTable Overflow (max=512)”
JNI local reference table summary (512 entries):
509 of Ljava/lang/Class; 164B (3 unique)
2 of Ljava/lang/String; 28B (2 unique)
1 of [Ljava/lang/String; 28B
Memory held directly by tracked refs is 576 bytes
Failed adding to JNI local ref table (has 512 entries)
,“509 of java.lang.class”对我来说看起来不太正确。 .如何在这里优化我的代码?
Perhaps I am attempting to do something I ought not.
I'm running a block of code in the emulator. It looks (more or less)
like this: http://pastie.org/1291380
This is to construct a live wallpaper background. I pass in a bitmap,
color palette, and tile array.The size of my bitmap is 256 x 256.
getRedPal / getGreenPal / getBluePal essentially does a call to
Color.red() / Color.green() / Color.blue() in order to get the rgb
color components of a palette object.
The loops chug along; I get all the way to the point where the j value
of drawInC hits 32, before the emulator crashes and burns:
11-11 15:34:44.032: INFO/distort_bmp(598): DrawInC: i: 0 j: 32
11-11 15:34:44.032: INFO/distort_bmp(598): DrawTiles: i: 0 j: 0
11-11 15:34:44.032: INFO/distort_bmp(598): DrawTiles: i: 0 j: 1
11-11 15:34:44.032: INFO/distort_bmp(598): DrawTiles: i: 0 j: 2
11-11 15:34:44.032: INFO/distort_bmp(598): DrawTiles: i: 0 j: 3
11-11 15:34:44.032: INFO/distort_bmp(598): DrawTiles: i: 0 j: 4
After which I get a dump file sent to /data/tombstones . Here is the
dump (but I sincerely don't find anything in it worth any value, just
a bunch of memory addresses): http://pastie.org/1291394
I added android:vmSafeMode="true" to my tag after
reading elsewhere that that could solve a problem. This is on 2.2,
using bitmap.h.
Personally I am dubious of that
jbyte* buffer =
(*env)->GetByteArrayElements(env, arr, &isCopy)
call; I plucked that
code from the net somewhere, as I was totally unable to get values
from my byte array "arr."
Any ideas?
EDIT
After manipulating my loop iterators (I shortened the number of loops), I now get an informative error:
"ReferenceTable overflow (max=512)"
JNI local reference table summary (512 entries):
509 of Ljava/lang/Class; 164B (3 unique)
2 of Ljava/lang/String; 28B (2 unique)
1 of [Ljava/lang/String; 28B
Memory held directly by tracked refs is 576 bytes
Failed adding to JNI local ref table (has 512 entries)
That "509 of java.lang.class" doesn't look too right to me...how can I optimize my code here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从该错误消息来看,某些本机代码似乎调用了返回 Class 对象的函数,并且已经执行了 509 次。其中 507 个调用返回了一个特定类。
JNI 本地引用让 GC 知道本机代码正在查看某个对象,因此即使其他地方没有对该对象的引用,也无法收集该对象。当本机代码返回到虚拟机时,这些本地引用将被释放。如果本机代码做了很多工作而不返回,则可能会溢出本地引用表。
您可能只需要在某处添加一个DeleteLocalRef。我的猜测是,由于 GetObjectClass 调用,您需要在 DrawTile 末尾添加一个。更好的是,将这些 GetMethodID 调用移至一次性设置函数。 (他们通过字符串搜索来查找方法,这使得它们不是特别快。)
有关更多信息,请参阅 JNI 提示。
From that error message, it appears that some bit of native code has called a function that returns a Class object, and has done so 509 times. 507 of those calls returned one particular class.
JNI local references let the GC know that native code is looking at an object, and therefore that object can't be collected even if there are no references to it elsewhere. These local refs are released when the native code returns to the VM. If the native code does a lot of work without returning, it's possible to overflow the local ref table.
You probably just need to add a DeleteLocalRef somewhere. My guess is you need to add one at the end of DrawTile, because of the GetObjectClass call. Better yet, move those GetMethodID calls to a one-time setup function. (They do string searches to find the method, making them not especially quick.)
For more info see JNI Tips.
我认为这可能是一个记忆问题。你释放数组吗?
如果您获得数组,
则必须在每个过程后释放 C 端的数组,否则您将填满内存,直到超过限制(限制大小取决于 Android 版本和/或制造商,但据我所知,每个过程最多 48MB)应用程序)
请参见此处:http://www.iam. ubc.ca/guides/javatut99/native1.1/implementing/array.html
顺便说一句,此方法确实将数组从 java 复制到 c 中的新内存块,您在那里工作,最后将其复制回java(在那之前该内存可能已移动到另一个位置)。为了提高性能,您可以查看 https://groups。 google.com/forum/?fromgroups#!msg/android-ndk/phDP5zqiYY4/BFMw4zTme8IJ
i think this might be a memory problem. do you release the arrays?
if you get arrays with
you must release the array on the c-side it after each procedure or you will fill your memory till you get over the limit (limit size depends on android version and/or manufacturer but is to my knowledge max 48MB per app)
see here: http://www.iam.ubc.ca/guides/javatut99/native1.1/implementing/array.html
btw, this method does copy the arrays from java to a new memory block in c, you work there and at the end its copied back to java (which memory might have been moved to another location till then). for performance improvement you might look at https://groups.google.com/forum/?fromgroups#!msg/android-ndk/phDP5zqiYY4/BFMw4zTme8IJ