频繁点击一个按钮,程序崩溃,查看log部分详情如下:
04-11 11:10:40.371: W/dalvikvm(5507): ReferenceTable overflow (max=1024) 04-11 11:10:40.371: W/dalvikvm(5507): JNI pinned array reference table (0x6242b008) dump: 04-11 11:10:40.371: W/dalvikvm(5507): Last 10 entries (of 1024): 04-11 11:10:40.371: W/dalvikvm(5507): 1023: 0x42e97298 int[] (456 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1022: 0x41c16618 int[] (69 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1021: 0x42e5ef28 byte[] (1280 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1020: 0x4230da28 byte[] (119 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1019: 0x422a03f0 byte[] (14 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1018: 0x42efced0 byte[] (1920 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1017: 0x422bc570 byte[] (119 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1016: 0x41f52e68 byte[] (14 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1015: 0x423c41d0 byte[] (119 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1014: 0x422b8bf0 byte[] (14 elements) 04-11 11:10:40.371: W/dalvikvm(5507): Summary: 04-11 11:10:40.371: W/dalvikvm(5507): 20 of byte[] (12 elements) (20 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of byte[] (13 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 489 of byte[] (14 elements) (489 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of byte[] (68 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 3 of byte[] (69 elements) (3 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 486 of byte[] (119 elements) (486 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 10 of byte[] (148 elements) (10 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 10 of byte[] (152 elements) (10 unique instances) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of byte[] (1280 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of byte[] (1920 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of int[] (69 elements) 04-11 11:10:40.371: W/dalvikvm(5507): 1 of int[] (456 elements) 04-11 11:10:40.371: E/dalvikvm(5507): Failed adding to JNI pinned array ref table (1024 entries) 04-11 11:10:40.371: I/dalvikvm(5507): "main" prio=5 tid=1 RUNNABLE
主要问题是ReferenceTable overflow 。查看资料,发现是引用计数器溢出了,最大为1024.即java的内存管理机制是基于引用的,需要计数器来记录引用的次数。在overflow这句话下面dump出了最近的10个引用,Summary下面可以看出有哪些引用。可以发现其中有两组byte[],每组的引用达到480多次。我恍然大悟,每点击一次按钮,引用一次,下次点击时上次的资源未被释放。于是一直累加直至计数器溢出。
检查代码发现:
int nArrLen_u = env->GetArrayLength(jbUrl); if(nArrLen_u > 0){ pUrl = (char *)malloc(nArrLen_u + 1); bzero(pUrl, (nArrLen_u + 1)); jU = env->GetByteArrayElements(jbUrl, NULL); memcpy(pUrl, jU, nArrLen_u); } int nArrLen_x = env->GetArrayLength(jbXml); if(nArrLen_x > 0){ pXmlBuf = (char *)malloc(nArrLen_x + 1); bzero(pXmlBuf, (nArrLen_x + 1)); jX = env->GetByteArrayElements(jbXml, NULL); memcpy(pXmlBuf, jX, nArrLen_x); }
代码上可以看出我调用了两次 GetByteArrayElements 但是却没有释放掉。所以在最后添上释放语句就解决了~
if(nArrLen_u > 0){ env->ReleaseByteArrayElements(jbUrl,jU,0); } if(nArrLen_x > 0){ env->ReleaseByteArrayElements(jbXml,jX,0); }
参考Android NDK之JNI陷阱 在用到NewByteArray类似方法时一定要DeleteLocalRef(),使用GetByteArrayElements时 要用ReleaseByteArrayElements来进行释放。
问题就是答案!真正明白是什么问题之后,也就找到了答案。
本文出自 “sunny4android” 博客,请务必保留此出处http://sunnyzlh.blog.51cto.com/10737636/1763549
原文地址:http://sunnyzlh.blog.51cto.com/10737636/1763549