Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause this memory issue?

I'm working on an app for windows phone 8, and I'm having a memory leak problem. But first some background. The app works (unfortunately) using WebBrowsers as pages. The pages are pretty complex with a lot of javascript involved.

The native part of the app, written in c#, is responsible for doing some simple communication with the javascript(e.g. native is a delegate for the javascript to communicate with a server), make animation for page transition, tracking, persistance, etc. All is done in a unique PhoneApplicationPage.

After I had some crashes for out of memory exceptions, I started profiling the app. I can see that the WebBrowsers, which are the big part of the the app, are being disposed correctly. But the problem I'm seeing is that memory continues to increase. What's worse, I have little feedback from the profiler. From what I understand, the profiler graph says there is a big problem, while the profiler numbers say there's no problem at all...

Note: the step represents a navigation from a WebBrowser to another WebBrowser. The spike is created (I suppose) by the animation between the two controls. In the span I've selected in the image, I was doing a navigation forward and one backward having a maxium of 5 WebBrowsers (2 for menus that are always there, 1 for the index page, 1 for the page I navigate from and 1 for the page I navigate to). At every navigation the profiler shows the correct number of WebBrowsers: 5 after navigating forward, 4 after navigating backward.

Note 2: I have added the red line to make clearer that the memory is going up in that span of time

As you can see from the image profiler the memory usage is pretty big but the numbers say it's low and in that span of time, retained allocation is lower than when it started...

I hope I've included enough information. I want some ideas on what could cause this problem. My ideas so far are:

-the javascript in the WebBrowser is doing something wrong (e.g. not cleaning some event handler). Even if this is the case, shouldn't the WebBrowser release the memory when it is destroyed?

-using a unique PhoneApplicationPage is something evil that is not supposed to be done, and changing its structure may cause this.

-other?

Another question: why does the graph show the correct amount of memory use while the number doesn't?

If you need more info about the profiler, ask and I will post them tomorrow.

like image 635
Fabio Marcolini Avatar asked Jul 31 '13 16:07

Fabio Marcolini


1 Answers

Ok after a lot of investigation I finally was able to find the leak. the leak is created by the WebBrowser control itself which seems to have some event handler that are not removed when you remove it from a Panel. In fact the leak is reproducible by following these steps:

  1. Create a new WebBrowser
  2. Add it to a Panel or whatever
  3. Navigate to a page, with an image which is big and heavy
  4. Tap somewhere in the blank space of the browser(tapping on the image seems to not create the leak)
  5. remove and collect the browser
  6. repeat from 1

at every iteration the memory of the image is never collected and the memory continue to grow.

A ticket to Microsoft was already sent.

The problem was resolved using a pool of WebBrowsers

like image 170
Fabio Marcolini Avatar answered Oct 02 '22 21:10

Fabio Marcolini