WebView存在的記憶體流失

來源:互聯網
上載者:User

標籤:mac   rgb   ack   ted   bsp   轉換   san   stat   extend   

## 0. Notice - earlier version

* 要使用WebView不造成記憶體流失,首先應該做的就是不能在xml中定義webview節點,而是在需要的時候動態產生。即:可以在使用WebView的地方放置一個LinearLayout類似ViewGroup的節點,然後在要使用WebView的時候,動態產生即:
```javaWebView mWebView = new WebView(getApplicationgContext()); LinearLayout mll  = findViewById(R.id.xxx); mll.addView(mWebView);```

然後一定要在onDestroy()方法中顯式的調用:

```javaprotected 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 
```javapublic 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 
```javapublic 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
```javapublic 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

WebView存在的記憶體流失

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.