xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.88k forks source link

[Bug] Memory Leak when Using Image in Certain Containers #12733

Open LGE-AUGMENSYS opened 3 years ago

LGE-AUGMENSYS commented 3 years ago

Description

When using the Xamarin.Forms Image control to display font glyphs inside a Syncfusion tab view, the entire pages are kept in memory, causing memory leaks.

Steps to Reproduce

  1. Launch the attached example app in Visual Studio using the "Memory Usage" performance profiling ;
  2. Click the "Leak Page" button;
  3. Navigate back to the main page;
  4. Gather a snapshot after some GC;
  5. Repeat step 2 & 3 several times (5 in the example screenshots);
  6. Repeat step 4 and make a diff between the two snapshots.

Expected Behavior

No delta in the diff of live instances or at least no additional instances of the LeakPage and its associated instances.

Actual Behavior

Many new instances are kept in memory and cannot be GC'ed. See screenshots below.

Basic Information

Screenshots

leak_summary leak_details

Reproduction Link

FormsMemoryLeak.zip

Workaround

Downgrading Xamarin.Forms. However, our product release would be heavily affected by this.

rmarinho commented 3 years ago

Does it work fine if you are not using Syncfusion tab view ?

LGE-AUGMENSYS commented 3 years ago

No. In our real application, it still leaks even if I remove the Syncfusion tab view. So I assume it has the same problem in some other containers. Due to our time pressure, I've only managed to recreate the issue using the tab view in the attached example.

LGE-AUGMENSYS commented 3 years ago

We found out that if we explicitly call Dispose on the Image control, the leaks are gone. However, other controls seem to have the same problem. So it looks like certain containers have problems propagating the disposal? Or did the disposal mechanism (or the way view hierarchical tree is managed) change after Xamarin.Forms 4.6?

hartez commented 3 years ago

We found out that if we explicitly call Dispose on the Image control, the leaks are gone. However, other controls seem to have the same problem. So it looks like certain containers have problems propagating the disposal?

What other "containers" are you using?

No. In our real application, it still leaks even if I remove the Syncfusion tab view. So I assume it has the same problem in some other containers.

If I remove the tab view and just have the Image as the content of the Page, there is no leak. So your theory that it's container-dependent seems sound, but right now the only example we have is the Syncfusion Tab. So unless you can tell us of another type of container which causes this issue, I think this is a Syncfusion bug, not a Forms bug.

LGE-AUGMENSYS commented 3 years ago

We found out that if we explicitly call Dispose on the Image control, the leaks are gone. However, other controls seem to have the same problem. So it looks like certain containers have problems propagating the disposal?

What other "containers" are you using?

No. In our real application, it still leaks even if I remove the Syncfusion tab view. So I assume it has the same problem in some other containers.

If I remove the tab view and just have the Image as the content of the Page, there is no leak. So your theory that it's container-dependent seems sound, but right now the only example we have is the Syncfusion Tab. So unless you can tell us of another type of container which causes this issue, I think this is a Syncfusion bug, not a Forms bug.

Other "containers" include mostly ContentView with dynamically loaded templates, Syncfusion list view, pull to refresh. I provided the example to Syncfusion and they claim to have fixed it (just the tab view, though). I am still waiting to see the fix.

However, one thing is also clear that the same setup (our app, Syncfusion controls) has no leaks in Forms 4.6 (details in the Basic Information section). Of course it could still be that some basic Forms concepts/APIs were misunderstood/misused and are now broken "as expected". I raised the same question to Syncfusion but unfortunately got no help.

I know it's a difficult ask, but I would really appreciate if you guys having more insights on the Forms version related changes could offer some hints or directions of thoughts.

LGE-AUGMENSYS commented 3 years ago

So I finally got some free time to further compare the differences between our leaking app and the example. It turns out the leaks are only present in a Master-Detail page setup. I'm linking the modified example app below. FormsMemoryLeak.zip

My testing is done using Visual Studio's builtin profiler. From the start page of the app:

  1. Navigate to the TitlePage ("Title Leak") and then back;
  2. Several explicit GCs, wait a bit and take a snapshot;
  3. Repeat step 1 five times;
  4. Several explicit GCs, wait a bit and take a snapshot;
  5. Diff those snapshots.

Snipaste_2020-12-07_15-45-38 Snipaste_2020-12-07_15-45-45

You can see there are exactly five additional instances of the TitlePage remaining. To summarize:

I hope this helps.