Skip to content

Memory Leak in ListView when UsingListViewCachingStrategy.RecycleElement [iOS] #27458

@NeilMalcolm

Description

@NeilMalcolm

Description

I'm seeing memory leaks regarding the ListView control in .NET 8/9. The issue appears on iOS when using ListViewCachingStrategy.RecycleElement, and is much worse when using this CachingStrategy with HasUnevenRows="true".

I have been unable to test on Android on .NET 9 but on .NET 8 (MAUI 8.0.93) I did not see this.

Popping the page with this markup will result in the entire Page remaining in memory.

<ListView HasUnevenRows="True"
          CachingStrategy="RecycleElement">
   <!-- Just using CachingStrategy="RecycleElement" produces some memory leaks 
        but not as much as both together-->
    <ListView.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>0</x:String>
            <x:String>1</x:String>
            <x:String>2</x:String>
            <x:String>3</x:String>
            <x:String>4</x:String>
            <x:String>5</x:String>
            <x:String>6</x:String>
            <x:String>7</x:String>
            <x:String>8</x:String>
            <x:String>9</x:String>
        </x:Array>
    </ListView.ItemsSource>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding .}"/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The reproduction I have done does not use Shell.

Steps to Reproduce

  1. Create a new .NET 9 MAUI App
  2. Define a static collection of WeakReference to track leaky views
  3. Add a new Page and a button to push the page to the navigation stack
  4. In the new page, add a ListView with HasUnevenRows="True" and CachingStrategy="RecycleElement".
  5. Either Bind or set the ItemsSource and define a simpleItemTemplate
  6. Add each item in the VisualElements to the leaky view collection when the page is disappearing
  7. Open the app
  8. Push and pop the page
  9. Force Garbage Collection
  10. See your leaky views remain in memory

You can see my reproduction repo, you can:

  1. Hit 'Open Leaks Issue # 1'
  2. Go back
  3. Hit 'Collect Garbage' a couple of times
  4. Hit 'Check References'
  5. See the alive references

Doing the same for Leaks Issue # 2 produces more leaky views.

Link to public reproduction project repository

https://github.com/NeilMalcolm/ListViewMemoryRepro

Version with bug

9.0.21 SR2.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

Not setting the CachingStrategy seems to resolve the issue.

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions