What could cause this memory issue?

我的梦境 提交于 2020-01-03 08:41:29

问题


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

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.


回答1:


I don't think There is enough information to find the cause to your leak, and without posting your entire solution I am not sure there can be, since the question is about locating the root cause of it...
What I Can offer is the approach I have used when I had my own memory leak.

The technique was to:

  1. Open a memory profiler. From your screenshot I see you are using one. I used perfmon. This article has some material about setting perfmon and @fmunkert also explains it rather well.
  2. Locate an area in the code that you suspect that it is likely that the leak is in that area. This part is mostly depending on you having good guesses about the part of the code that is responsible for the issue.
  3. Push the Leak to the extreme: Use labels and "goto" for isolating an area / function and repeat the suspicious code many times (a loop will work to. I find goto more convenient for this matter).
  4. In the loop I have used a breakpoint that halted every 50 hits for examining the delta in the memory usage. Of course you can change the value to feet a noticeable leak change in your application.
  5. If you have located the area that causes the leak, the memory usage should rapidly spike. If the Memory usage does not spike, repeat stages 1-4 with another area of code that you suspect being the root cause. If it does, continue to 6.
  6. In the area you have found to be the cause, use same technique (goto + labels) to zoom in and isolate smaller parts of the area until you find the source of the leak (Please don't downvote me for the recursive step... :0) ).

Please note that the down sides of this method are:

  1. If you are allocating an object in the loop, it's disposal should be also contained in the loop.
  2. If you have more than one source of leak, It makes it harder to spot (yet still possible)

Please leave a comment if any further explanations are required.
Good luck...




回答2:


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




回答3:


Did you clean up your event handlers? You may inadvertently still have some references if the controls are rooted.



来源:https://stackoverflow.com/questions/17975488/what-could-cause-this-memory-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!