androidandroid-webviewandroid-memoryandroid-websettings

memory is increase when click on webview


I load website which loads whole page using Javascript files to WebView. Every time when I load page to Web view it shows me some memory increased in

setting > apps > running

Even, when I release Webview then also it does not release the memory.

I am not caching the resources in Webview. But, when I used instruments to analyse the memory , it shows me images resources are cached in memory which does not get released. My code is here for ref :

    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    Intent in = getIntent();
    String page_url = in.getStringExtra("link");
    Log.i("display","url :"+page_url);
    if(deleteDatabase("webview.db") && deleteDatabase("webviewCache.db")){
        Toast.makeText(this, "Delete Webvie data base successfully",Toast.LENGTH_SHORT).show();
    }
    webview = (WebView) findViewById(R.id.webpage);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setAppCacheEnabled(false);
    webview.loadUrl(page_url);
    webview.clearHistory();
    webview.clearFormData();
    webview.clearCache(true);

Can anyone please guide me where I am missing? How can I optimize the memory loading to WebView?

Thanks in advance.


Solution

  • WebView is a complex UI widget and will consume significant memory whilst in use. Prior to Andriod 4.4, the WebView.freeMemory() API may help, depending on what is actually using the memory behind the scenes.

    Some memory will be reclaimed when you are finished with the WebView and invoke WebView.destroy() and detach it from the view hierarchy.

    In Android 4.4 memory management is more automatic inside the WebView and the WebView will attempt to free up memory when the system is under load and triggers onTrimMemory (http://developer.android.com/reference/android/content/ComponentCallbacks2.html#onTrimMemory(int)).