Solution to memory leak in Android WebView

Solution to memory leak in Android WebView

Recently, a large number of pictures and texts were displayed in the activity nested WebView. It was found that the app memory was rising and could not free the memory. After checking a lot of data, it was probably a bug in the WebView that referenced the activity, resulting in memory leakage, so I tried to pass getapplicationcontext.

1. Avoid directly writing WebView control in XML, which will reference activity, so write a LinearLayout in XML, and then linearlayout.addview (New mywebview (getapplicationcontext());

In this way, the dynamic generation of WebView can avoid memory leakage, but this will lead to exceptions and program crashes when clicking on hyperlinks in WebView of some models. The temporary solution is to prohibit clicking and rewrite WebView,

This avoids program crashes.

2. The WebView memory needs to be released manually when the activity is closed

The above method can release the memory, but it has a defect, that is, it can't click the content of WebView. Another method is to open another process for the activity of nested WebView and display it as an independent process

The above practical examples of the solution to the memory leakage of Android WebView are explained in detail. If you have any questions, please leave a message for discussion and make common progress. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>