我的Android 4.0.4應用程序由一個WebView組成,用戶可以通過該WebView查看本地存儲在資產目錄中的多個頁面。當通過頁面循環,下面的錯誤,最終被觸發和應用程序崩潰:Android WebView資產引用內存泄漏
- JNI錯誤(應用程序錯誤):本地參照表溢出(最大值= 512)
- 無法添加到JNI本地參考表(具有512個條目)
- VM中止
- 致命信號11(SIGSEGV)在0xdeadd00dd(代碼= 1)
這個問題似乎涉及到此處報道的那些:
- WebView memory leak in android app
- Android WebView Memory Leak when using Assets
- Viewpager Webview memory issue
我用在下面的鏈接中給出的內存分析工具插件指令,以檢查細節:
http://therockncoder.blogspot.ca/2012/09/fixing-android-memory-leak.html
結果顯示如下(不能發佈屏幕截圖上課的是,這樣的文本將不得不這樣做):
MAT摘錄
Class Name | Shallow Heap | Retained Heap | Percentage
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| | |
java.lang.Thread @ 0x40daa320 Thread-39775 Thread | 80 | 15,310,552 | 76.74%
|- byte[32768] @ 0x40d5a8d0 <!DOCTYPE html>.<html xml:lang="">.<head>. <title>Android Test-HTML5-480PX-Page 0</title>. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />. <meta name="viewport" content="width=360, height=480">. <!--. <meta name="viewport" co...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x40e25bb8 on-play-state: running;. -webkit-animation-timing-function: step-start;. }. @-webkit-keyframes ag16780-anim45051. {. 0.000% { background-position:0px 0px,77px 0px,77px 0px,77px 0px,77px 0px,77px 0px; }. 20.000% { background-position...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x40e60520 //<![CDATA[.var pageParams = new Object();.pageParams['readMode'] = 1;...function applyReadMode().{. var audioNodes = document.getElementsByClassName('BGAudio');. for (var i=0; i<audioNodes.length; i++). {. .if (pageParams['readMode'] == 0). ...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x40e86f48 .PNG........IHDR....... ......U......gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....bKGD..............pHYs...#...#.x.?v...ZIDATH..U]l\W..f.9.......8..G.....Z$ZUi..*EQQ...RE.Hi.D".0BTj..J.x..O.J..C.)..IU.R......HB.8..&..\.x..w..{fx...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x40f09f00 html.{. padding : 0px 0px 0px 0px;. margin : 0px 0px 0px 0px;. height : 100%;. width : 100%;. background : #ffffff;.}.body.{. padding : 0px 0px 0px 0px;. margin : 0px 0px...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x40f11f18 .=Xt.....H...hE:t.;.......=.s....f.(.....v.5'<.8}=.=kXF..&&...K...j.........<...A..........}.......c.c..7.e{b.....O.p..h....e.1....8.zd{..........}3.Z.W..v.|}y.u...3M.....h2IDAT...........Z.;u..M.....'!.(.S.....|j.]..h.l7.... .....I...u.&J5.";9.d04.S.........| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41009af0 //<![CDATA[.var pageParams = new Object();.pageParams['readMode'] = 1;...function applyReadMode().{. var audioNodes = document.getElementsByClassName('BGAudio');. for (var i=0; i<audioNodes.length; i++). {. .if (pageParams['readMode'] == 0). ...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41011b08 html.{. padding : 0px 0px 0px 0px;. margin : 0px 0px 0px 0px;. height : 100%;. width : 100%;. background : #ffffff;.}.body.{. padding : 0px 0px 0px 0px;. margin : 0px 0px...| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41019b20 ...t.%#2#b......0..0.6.....A.M....%.F,.*...(.>.q$_.0... a..sF...."Ypn"....#[email protected]).F....4.....Q.1.Wd..3.|.Y%........:.w.F~ ]..0i>a......4n.E7.O..+.7S...D...|.IDAT..'...<.....E.n...!.1.....Tx211..E....4 .*f....>..)..)...gS.j.. WX....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41021b38 .PNG........IHDR...|.................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....PLTE.................................................................................................................................................| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41029b50 .PNG........IHDR.......Q.......0.....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....bKGD..............pHYs...#...#.x.?v..:.IDATx...gx...........-.....4...p...S....%$!$.$$....z....8T.:[email protected]| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41031b68 .PNG........IHDR...V.........0..X....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....bKGD..............pHYs...#...#.x.?v....IDATh..Zyp.U....f.;3....j0..p...E]...M...*%..,.J..Pb..,....Z.AE.E.....n.."[email protected]>...........W......| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410889d0 .PNG........IHDR.............a.~e....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....PLTE...Q_HRgL......=I93?/f~Zg~[[email protected])0$'2&(3&/<('1$......""!:;:"#"......)3&...............&&&'''.......... ...........$.677888.$....................| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410909e8 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...wPLTE........~..............v...........~.........{yn........................................................F.. ..4..............!...........+..l....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x41098a00 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...wPLTE...uuj....................i.......................................................................S..'..$..............*..............[..........| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410a0a18 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...tPLTE...llh...........................|.u.....................................................]..*..+..............-..............T..............M....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410a8a30 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...wPLTE...ooi.......................}....................................................................[..)..%..............+..............Y..........| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410b0a48 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...zPLTE...wvk.......................{....................................................................\..(..$..............,..............\..........| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410b8a60 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...zPLTE...rqg.....d...................................z........................................................R..%.. ..............(..............]....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410c0a78 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...wPLTE...rrh........r............................................................................................Q..%.. ..............(................| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410c8a90 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...wPLTE........}..............O...........|.........xwl........................................................G..!.....8.............."...........'....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410d0aa8 .PNG........IHDR.....................gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...zPLTE........~..............m...........~.........{yn........................................................F.. ..4..............!...........+..l....| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410d8ac0 .PNG........IHDR.............e.. ....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...yPLTE...mmmhfZ........h.................)........c..kppp..h..u.......................................................................x..o..a..........| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410e0ad8 .PNG........IHDR.............e.. ....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<....PLTE...hhh.......................-...........e..~........|.............................................................................s.............| 32,784 | 32,784 | 0.16%
|- byte[32768] @ 0x410e8af0 .PNG........IHDR.............e.. ....gAMA......a.....sRGB........ cHRM..z&..............u0...`..:....p..Q<...|PLTE.........,,+........c..~...........]..............y....................n.........................................................................| 32,784 | 32,784 | 0.16%
'- Total: 25 of 471 entries; 446 more | | |
android.widget.HorizontalScrollView @ 0x40e1d8b8 | 576 | 978,456 | 4.90%
class android.content.res.Resources @ 0x40ab1570 System Class | 48 | 266,432 | 1.34%
...
日誌摘要
...
10-08 22:10:28.970: D/MediaPlayer(9823): pause() out
10-08 22:10:29.090: E/dalvikvm(9823): JNI ERROR (app bug): local reference table overflow (max=512)
10-08 22:10:29.090: W/dalvikvm(9823): JNI local reference table (0x2498270) dump:
10-08 22:10:29.090: W/dalvikvm(9823): Last 10 entries (of 512):
10-08 22:10:29.090: W/dalvikvm(9823): 511: 0x40d696d0 android.content.res.AssetManager
10-08 22:10:29.090: W/dalvikvm(9823): 510: 0x4201eab0 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 509: 0x42016a98 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 508: 0x4200ea80 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 507: 0x42006a68 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 506: 0x41ffea50 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 505: 0x41ff6a38 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 504: 0x41feea20 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 503: 0x41fe6a08 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): 502: 0x41fde9f0 byte[] (32768 elements)
10-08 22:10:29.090: W/dalvikvm(9823): Summary:
10-08 22:10:29.090: W/dalvikvm(9823): 1 of java.lang.Class
10-08 22:10:29.090: W/dalvikvm(9823): 510 of byte[] (32768 elements) (510 unique instances)
10-08 22:10:29.090: W/dalvikvm(9823): 1 of android.content.res.AssetManager
10-08 22:10:29.090: E/dalvikvm(9823): Failed adding to JNI local ref table (has 512 entries)
10-08 22:10:29.090: I/dalvikvm(9823): "Thread-39898" prio=5 tid=12 RUNNABLE
10-08 22:10:29.090: I/dalvikvm(9823): | group="main" sCount=0 dsCount=0 obj=0x40d6b9d8 self=0x222a030
10-08 22:10:29.090: I/dalvikvm(9823): | sysTid=9871 nice=0 sched=0/0 cgrp=default handle=29456640
10-08 22:10:29.090: I/dalvikvm(9823): | schedstat=(520906000 142824000 757) utm=22 stm=30 core=2
10-08 22:10:29.090: I/dalvikvm(9823): at android.content.res.AssetManager.readAsset(Native Method)
10-08 22:10:29.090: I/dalvikvm(9823): at android.content.res.AssetManager.access$700(AssetManager.java:35)
10-08 22:10:29.090: I/dalvikvm(9823): at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:648)
10-08 22:10:29.090: I/dalvikvm(9823): at dalvik.system.NativeStart.run(Native Method)
10-08 22:10:29.090: E/dalvikvm(9823): VM aborting
10-08 22:10:29.090: A/libc(9823): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
10-08 22:10:29.140: I/MediaPlayer(9823): setLPAflag() in
...
通過查看MAT條目,看來主線程持有對所有HTML頁面,CSS和Javascript文件,圖像和音頻文件的引用,並且不會釋放它們。最後,當調用新URL時,AssetManager嘗試寫入超出JNI本地引用表的邊界(512個條目)的條目,耗盡可用空間並導致內存泄漏。
我試過各種各樣的事情,以防止保留引用,沒有運氣。這包括:
初始化的WebView,這樣它不緩存的網頁:
mWebView.getSettings().setAppCacheEnabled(false); mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); mWebView.setDrawingCacheEnabled(false);
試圖加載下一個頁面之前刪除所有現有的WebView數據:
mWebView.stopLoading(); mWebView.clearCache(true); mWebView.destroyDrawingCache(); mWebView.clearHistory(); mWebView.freeMemory(); mWebView.getInstance().deleteAllData();
動態創建WebView而不是通過res/layout XML,併爲其分配應用程序上下文而不是活動上下文:
mWebView = new WebView(getApplicationContext());
破壞當前的WebView改變頁面和動態創建下一個頁面一個新的人之前,但是這比減慢應用程序的其他沒有影響; JNI的本地表仍保留舊的引用:
mWebViewContainer.removeViewAt(0); mWebView.destroyDrawingCache(); mWebView.destroy(); mWebView = null; System.gc();
理想的事情將是消除在JNI表中的舊條目加載下一個頁面之前,但我發現沒有辦法的事那 - 一旦URL被調用,資產目錄引用就會被保留,儘管我已經嘗試了。也許我錯過了一些東西,或者在稍後的Android版本中有待解決?否則,即使爲每個頁面銷燬和創建新的WebView都會對性能產生負面影響,並且會使設計複雜化,如果頁面可以在自己的Thread中創建並保存自己的資產引用而不是主線程,那可能會起作用只要這些線程可以在頁面更改期間停止)。
更新
我想在單獨的線程創建我的網頁視圖,並得到了錯誤「所有的WebView方法必須在UI線程中調用。WebView中的未來版本可能不支持在其他線程使用。「。我假設'UI線程'指的是主線程。此公告似乎支持:
A WebView in a thread can't be created
@VicTom我也中遇到的工作 'JNI ERROR(APP錯誤):本地參照表溢出(最大值= 512)' 的錯誤。 –