WebView-存在的内存泄漏
2024-10-20 09:29:30
0. Notice - earlier version
- 要使用WebView不造成内存泄漏,首先应该做的就是不能在xml中定义webview节点,而是在需要的时候动态生成。即:可以在使用WebView的地方放置一个LinearLayout类似ViewGroup的节点,然后在要使用WebView的时候,动态生成即:
WebView mWebView = new WebView(getApplicationgContext());
LinearLayout mll = findViewById(R.id.xxx);
mll.addView(mWebView);
然后一定要在onDestroy()方法中显式的调用:
protected void onDestroy() {
super.onDestroy();
mWebView.removeAllViews();
mWebView.destroy()
}
注意: new WebView(getApplicationgContext()) ;必须传入ApplicationContext如果传入Activity的Context的话,对内存的引用会一直被保持着。有人用这个方法解决了当Activity被消除后依然保持引用的问题。但是你会发现,如果你需要在WebView中打开链接或者你打开的页面带有flash,获得你的WebView想弹出一个dialog,都会导致从ApplicationContext到ActivityContext的强制类型转换错误,从而导致你应用崩溃。
1. What leads to Memory leak
1.1 InnerClass
public class InnerClassActivity extends Activity{
private static Leak mLeak;
class Leak {
int a = 3;
private Context mLeakContext;
Leak(Context context) {
mLeakContext = context;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mLeak = new Leak(this);
Toast.makeText(this, "This is InnerClassActivity", Toast.LENGTH_SHORT).show();
}
}
1.2 Singleton
public class Singleton {
private static Singleton instance;
private Context mContext1;
private Singleton(Context context) {
this.mContext1 = context;
}
public static Singleton getInstance(Context context) {
if(instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton(context);
}
}
}
return instance;
}
}
1.3 webview - earlier version
public class WebViewCreateActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_create);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
WebView webView = new WebView(this);
webView.setLayoutParams(layoutParams);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl("https://www.baidu.com/");
ll.addView(webView);
Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
}
}
2. How to detect the Memory Leak
2.1 Android Studio
- GC manually
- dump Java Heap
2.2 MemoryLeak in Our Project
品牌 | 固件 | 泄漏点 |
---|---|---|
三星 | 4.0.4 | LightAppManager |
小米 | 5.0.1 | LightAppManager/ mAccessibilityManager |
华为 | 6.0 | LightAppManager/ 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
最新文章
- jsp使用EL表达式回传boolean值出错的问题
- 第一章 web应用程序开发原理
- OKHttp
- Uva11374 Airport Express
- ibatis中iterate的用法(conjunction=";or"; ";,";)
- Tornado,表单处理,一样在行
- Django中的Model(操作表)
- ios NSMethodSignature and NSInvocation 消息转发
- C#4.0中var和dynamic的区别
- SOFTWARE_INTRODUCE_01
- 记一次排查log4net 不输出日志的解决过程
- getRequestURI()与getRequestURL()的区别
- 线上服务器上安装的VNCServer不能正常工作
- ioctl函数
- Tars http服务
- JavaScript——JS屏蔽F12和右键
- BZOJ3531 树剖 + 动态开点线段树
- JQuery续
- 自学Linux Shell11.3-使用变量
- how-to-convert-string-to-localdate
热门文章
- [LeetCode] Longest Palindromic Substring 最长回文串
- C#中两个Form窗口之间的传值(父->;子)(子->;父)
- 自建Ceph存储与 AWS、阿里云、腾讯云的成本对比
- android studio 集成微信登录
- JAVA+Maven+TestNG搭建接口测试框架及实例
- --关于null在oracle数据库中是否参与计算,进行验证,
- 北京培训记day3
- Pi 前2600位
- java 的public private protected作用域
- CentOS 7 防火墙 firewall 的使用