Memory leak with simplest back/forth navigation in Forms Android App

JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

I'm investigating a memory leak (or leaks) which seem to be core to our Forms app.

We're using FreshMvvm, which previously had a pretty bad leak due to Pages/PageModels not being garbage collected. Michael Ridland (FreshMvvm) author) fixed that, which is great.

However, we're still seeing the figure returned by GC.GetTotalMemory() steadily increase. It does decrease, but the general trend is upwards.

I've stripped things right back so that I've just got a plain Page which pushes another page and we can then pop back to the first.

Doing this, I'm seeing GC.GetTotalMemory() generally increase by 2K - 4K every cycle. This is after forcibly calling GC.Collect();

We're running Xamarin.Forms 2.3.4.270 - I know this is quite old, but we really don't want to upgrade as we don't want to introduce 'new Forms' bugs! However, I have tried experimentally upgrading to 2.5.0.121934, and I see the same behaviour (as well as aspects of the app stop working with this version).

Short of using Profiler, are there any investigative techniques I can use to find the culprit?

My next step will be to strip more and more out!

Answers

  • Jacob.MaristanyJacob.Maristany USUniversity ✭✭

    I've seen this same thing and did some profiling yesterday. Using Forms versions 2.4 and 2.5, I found that (when using a NavigationPage), Pages never leave the heap. Ever. I'm working today to compile more information on the matter to prove I'm not crazy.

  • batmacibatmaci DEMember ✭✭✭✭✭

    Do you guys have any results? I am having the same problem. whatever I tried, it doesnt work, pages and pagemodels are always in the memory. setting bindingcontext=null doesnt even work. I am suspecting freshmvvm is the problem. I will try to create and app without using it to see the actual problem. please share your experience

  • Jacob.MaristanyJacob.Maristany USUniversity ✭✭

    @batmaci
    Sorry, I forgot to update my post. I was crazy! I was so burned out on profiling my app, I stopped taking snapshots and just looked at the raw allocations! When profiling, always make sure to take a snapshot and use that to compare to a previous snapshot or to see what's allocated at that time. If you just look at allocations, it's a compilation of everything allocated ever... not what's currently allocated.

  • batmacibatmaci DEMember ✭✭✭✭✭

    @Jacob.Maristany i am not even using xamarin profiler. I use VS2017 profiler on UWP and it makes me crazy also. when navigate back from a page and take snapshop on other page, snapshot shows that previous page is still in the memory and there is a cycle detected label on it. I tried totally empty page with a label and it still not removed from memory. It doesnt look like something to do with my code but all leads me again freshmvvm navigation but i will update when I am 100% sure.

  • batmacibatmaci DEMember ✭✭✭✭✭
  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2018

    One thing I have found very useful during development is to have my app keep track of which pages have been instantiated but not yet finalised, and to have an option available from the app's first page to force garbage collection and then display the contents of that instantiated but not finalised list.

    I also, during development, have an option that navigates all pages of the app, pushing and popping as required. Whilst that doesn't execute all functionality, combining that with the check for unfinalised pages is a quick way of identifying pages with fundamental problems in terms of memory leakage.

    When a page is left unfinalised, I go in with the profiler (and code review) to find and then resolve the cause.

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 2018

    @JamesLavery - did you ever resolve this?

    As mentioned previously, I have various bits of self-checking in my code during development, including keep a count of newed but not finalized objects, including pages, custom controls, dependency services, renderers and value converters.

    On UWP, I'm seeing all of the various counts being zero when I force a cleanup. However, on Android and iOS, I see multiple renderer instances newed but not finalised (all other counts, including custom controls that the renderers are for, being zero). I've reviewed the renderer code and commented out anything other than calls to base - that made no difference.

    I am still using XF 2.4 - there were too many issues reported against 2.5 to move to that, and a major version number change to 3.x cannot be contemplated until there's a really strong reason to make the move.

    I do wonder what assembly: ExportRenderer does (or more accurately what System.Attribute does), whether in some way that could be causing the issue. Not sure there's a way of un-exporting when finished with something.

    @AdamP - I know you wrote about custom renderers a while back. Did you ever see this?

    cc @robbit

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    We didn't resolve it. We had other larger leaks due to Freshmvvm which were fixed (as I mentioned), but ended up having to live with it. I'll check the code to see if we ended up with mitigations in place - can't remember right now.
Sign In or Register to comment.