Skip to content

Memory Increase Issues When Using ListView in C# MAUI .NET 8.0 Projects #27386

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.

namespace MauiListView
{
    public class Message
    {
        public string? Content { get; set; }
        public DateTime Timestamp { get; set; }
    }

    public partial class MainPage : ContentPage
    {
        private const int MAX_MESSAGES = 20;
        private List<Message> messages;
        private CancellationTokenSource? cancellationTokenSource;

        public MainPage()
        {
            InitializeComponent();
            messages = new List<Message>();
            MessageListView.ItemsSource = messages;

            StartMessageGeneration();
        }

        private async void StartMessageGeneration()
        {
            cancellationTokenSource = new CancellationTokenSource();

            try
            {
                while (!cancellationTokenSource.Token.IsCancellationRequested)
                {
                    AddMessage($"새로운 메시지 {DateTime.Now}");
                    await Task.Delay(1000, cancellationTokenSource.Token);
                }
            }
            catch (OperationCanceledException)
            {
                
            }
        }

        private void AddMessage(string content)
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                var message = new Message
                {
                    Content = content,
                    Timestamp = DateTime.Now
                };

                messages.Insert(0, message);

                while (messages.Count > MAX_MESSAGES)
                {
                    messages.RemoveAt(messages.Count - 1);
                }

                MessageListView.ItemsSource = null;
                MessageListView.ItemsSource = messages;
            });
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            cancellationTokenSource?.Cancel();
        }
    }

}

Original Comments

Feedback Bot on 1/22/2025, 11:32 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-controls-listviewListView and TableViewp/2Work that is important, but is currently not scheduled for releaseperf/memory-leak 💦Memory usage grows / objects live forever (sub: perf)platform/windows

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions