locked
Memory Leak using InvokeScriptAsync for WebView

    Question

  • I'm updating Webview content frequently using InvokeScriptAsync method.  With each call memory keeps going up and never goes back down.  Since I'm sending large data and quite often the app can reach above 1.5Gb in memory within few minutes.  Is there anyway to force Garbage Collect on webview manually?

    My code:

    webView.Navigate(new Uri("ms-appx-web:///html/main.html")) 

    private async void createMemoryLeak(string execute, JsonObject json){

    var memoryLeak = await webView.InvokeScriptAsync("eval", new string[] { "\"something that does nothing to DOM, but has really long string to cause memory leak faster. With every click memory keeps going up.\"" });

    }

    This is deal breaker, so any help will be appreciated.

    Thanks

    Monday, February 2, 2015 4:17 AM

All replies

  • Hi Mark472,

    Interesting question, I will create a webview and using your code to do some test.

    Will update later after I have some test. Thanks for your understanding.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, February 3, 2015 2:10 AM
    Moderator
  • Hi Mark,

    Here update some findings.

    I can reproduce the behavior by your code with 10,000 time eval.

                for (int i = 0; i < 10000; i++)
                {
                    var memoryLeak = await webView.InvokeScriptAsync("eval", new string[] { "\"something that does nothing to DOM, but has really long string to cause memory leak faster. With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.With every click memory keeps going up.\"" });
                }

    I tried to clean the webview control programmatically by remove it by code and do GC.collect() but seems the memory is still not released.

                //var t =  container.Children[0];
                container.Children.RemoveAt(0);    
                GC.Collect();

    Probably the issue is related with cache, however looks currently there is no good explanation or , take a look at Matt's blog: How to clear the WebView cache.

    I can confirm this could be a memory issue, I will also report this to our seniors, please be patient.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 3, 2015 3:33 AM
    Moderator
  • Hi Mark,

    Just consult with our seniors, a really key question might be why do you want to update Webview content frequently using InvokeScriptAsync method, besides why you would like to sending large data to WebView.

    If you could share you scenario with us, we can try to provide a better suggestion. Inject too much things into WebView is not best practice.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 17, 2015 8:25 AM
    Moderator