Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Example WebViewJavaScriptInterface memory issue

$
0
0

Hello,

I am trying to understand how to properly use the Android WebView from the memory usage/GC standpoint. I downloaded the example project WebViewJavaScriptInterface, built and ran the app, then started the Android DDMS monitor to look at the heap allocation. Initially, the heap size starts at 3.6MB with 3.1MB allocated. After several rotations, the heap allocation and heap size quickly grows to 10MB+. I modified the example to start a timer that causes the device orientation to rotate 1 second after OnCreate in order to accelerate the testing.

Just to confirm, I went and recreated a similar WebView app in native Android Java. In the native app, the screen rotates repeatedly and the heap size never grows beyond ~5.8MB. I can see in DDMS the heap allocations growing and then reclaiming regularly, keeping the allocated memory ~4MB.

Why does the allocated heap memory increase continually in the MonoDroid example app? I tried including calls to Destroy() and Dispose() in an attempt to reclaim memory in the OnDestroy method, but this didn't seem to help. I also tried instantiating the WebView programmatically, using the ApplicationContext rather than the Activity context as others have mentioned that this might help. My modified Activity's code is below.

[Activity (Label = "Mono WebView ScriptInterface", MainLauncher = true)]
public class JavaScriptInterfaceActivity : Activity
{
    const string html = @"
<html>
<body>
<p>This is a paragraph.</p>
<button type=""button"" onClick=""Foo.bar('test message')"">Click Me!</button>
</body>
</html>";

    private WebView view;
    private System.Timers.Timer t;

    private void TimerElapsed(object sender, System.Timers.ElapsedEventArgs args) {
        t.Stop();
        if (this.RequestedOrientation == Android.Content.PM.ScreenOrientation.Portrait)
        {
            this.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
        }
        else
        {
            this.RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
        }
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        LinearLayout container = FindViewById<LinearLayout> (Resource.Id.container);
        view = new WebView (this.ApplicationContext);//FindViewById<WebView> (Resource.Id.web);
        container.AddView (view);
        view.Settings.JavaScriptEnabled = true;
        view.SetWebChromeClient (new WebChromeClient ());
        //view.AddJavascriptInterface (new Foo (this), "Foo");
        view.LoadData (html, "text/html", null);

        t = new System.Timers.Timer(1000);
        t.Elapsed += TimerElapsed;
        t.Start ();
    }

    protected override void OnPause ()
    {
        base.OnPause ();
    }
    protected override void OnDestroy ()
    {
        base.OnDestroy ();
        LinearLayout container = FindViewById<LinearLayout> (Resource.Id.container);
        container.RemoveAllViews ();
        view.DestroyDrawingCache ();
        view.Destroy ();
        view.Dispose ();

        t.Elapsed -= TimerElapsed;
        t = null;

        view = null;
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>