WebView-存在的内存泄漏

本文介绍如何避免使用WebView造成的内存泄漏,包括不在XML中定义WebView节点、正确销毁WebView实例的方法,并探讨了导致内存泄漏的一些常见原因如内部类和单例模式的不当使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. Notice - earlier version

  • 要使用WebView不造成内存泄漏,首先应该做的就是不能在xml中定义webview节点,而是在需要的时候动态生成。即:可以在使用WebView的地方放置一个LinearLayout类似ViewGroup的节点,然后在要使用WebView的时候,动态生成即:

 
  1. WebView mWebView = new WebView(getApplicationgContext());
  2. LinearLayout mll = findViewById(R.id.xxx);
  3. mll.addView(mWebView);

然后一定要在onDestroy()方法中显式的调用:


 
  1. protected void onDestroy() {
  2. super.onDestroy();
  3. mWebView.removeAllViews();
  4. mWebView.destroy()
  5. }

注意: new WebView(getApplicationgContext()) ;必须传入ApplicationContext如果传入Activity的Context的话,对内存的引用会一直被保持着。有人用这个方法解决了当Activity被消除后依然保持引用的问题。但是你会发现,如果你需要在WebView中打开链接或者你打开的页面带有flash,获得你的WebView想弹出一个dialog,都会导致从ApplicationContext到ActivityContext的强制类型转换错误,从而导致你应用崩溃。

1. What leads to Memory leak

1.1 InnerClass


 
  1. public class InnerClassActivity extends Activity{
  2. private static Leak mLeak;
  3. class Leak {
  4. int a = 3;
  5. private Context mLeakContext;
  6. Leak(Context context) {
  7. mLeakContext = context;
  8. }
  9. }
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.test);
  14. mLeak = new Leak(this);
  15. Toast.makeText(this, "This is InnerClassActivity", Toast.LENGTH_SHORT).show();
  16. }
  17. }

1.2 Singleton


 
  1. public class Singleton {
  2. private static Singleton instance;
  3. private Context mContext1;
  4. private Singleton(Context context) {
  5. this.mContext1 = context;
  6. }
  7. public static Singleton getInstance(Context context) {
  8. if(instance == null) {
  9. synchronized (Singleton.class) {
  10. if (instance == null) {
  11. instance = new Singleton(context);
  12. }
  13. }
  14. }
  15. return instance;
  16. }
  17. }

1.3 webview - earlier version


 
  1. public class WebViewCreateActivity extends Activity{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.webview_create);
  6. LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
  7. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  8. WebView webView = new WebView(this);
  9. webView.setLayoutParams(layoutParams);
  10. WebSettings webSettings = webView.getSettings();
  11. webSettings.setJavaScriptEnabled(true);
  12. webSettings.setDomStorageEnabled(true);
  13. webView.loadUrl("https://www.baidu.com/");
  14. ll.addView(webView);
  15. Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
  16. }
  17. }

2. How to detect the Memory Leak

2.1 Android Studio

  • GC manually
  • dump Java Heap

2.2 MemoryLeak in Our Project

品牌固件泄漏点
三星4.0.4LightAppManager
小米5.0.1LightAppManager/ mAccessibilityManager
华为6.0LightAppManager/ mAccessibilityManager
2.2.1 LightAppManager泄漏

3次手动GC后,内存的增长:

泄漏点1: LightAppManager 泄漏

泄漏点2: WebView-wrapper getSystemService泄漏

3. 官方态度

It's 2016 now and, as far as I can see it, the issue still hasn't been resolved. I tested it on Nexus 5 and Nexus 6 with the latest WebView updates (since the component is now separate from the OS itself). Could someone, please, take a look at this issue?!

来源: https://code.google.com/p/android/issues/detail?id=9375

转载于:https://www.cnblogs.com/linkun/p/6254479.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值