您好,登錄后才能下訂單哦!
頻繁點擊一個按鈕,程序崩潰,查看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 。查看資料,發(fā)現(xiàn)是引用計數(shù)器溢出了,最大為1024.即java的內(nèi)存管理機制是基于引用的,需要計數(shù)器來記錄引用的次數(shù)。在overflow這句話下面dump出了最近的10個引用,Summary下面可以看出有哪些引用??梢园l(fā)現(xiàn)其中有兩組byte[],每組的引用達到480多次。我恍然大悟,每點擊一次按鈕,引用一次,下次點擊時上次的資源未被釋放。于是一直累加直至計數(shù)器溢出。
檢查代碼發(fā)現(xiàn):
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); }
代碼上可以看出我調(diào)用了兩次 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來進行釋放。
問題就是答案!真正明白是什么問題之后,也就找到了答案。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。