CommunityToolkit / CommunityToolkit/Labs-Windows

Memory leaking in MarkdownTextBlock

Open
#759 0 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
482
Forks
89
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

A memory leak occurs in `MarkdownTextBlock` when the Text property is bound to a live stream.
The same update logic works correctly with `TextBox` and does not exhibit any memory issues.
- Using MarkdownTextBlock causes a memory leak.
The private bytes start at around 100 MB and grow to over 3 GB after loading the entire text.
- Using a TextBox instead of MarkdownTextBlock does not cause a memory leak.
The private bytes start at around 100 MB and only increase to about 150 MB after loading the entire text.

##### xaml code:

```xaml
























```

##### csharp code:
```csharp
public partial class BaseMessage : ReactiveObject
{
[Reactive]
private string _messageText = string.Empty;
}
public partial class ModelMessage : BaseMessage
{
public Task SubscribeAsync(IAsyncEnumerable strings, CancellationToken cancellationToken)
{
var observable = strings
.ToObservable()
.Buffer(TimeSpan.FromMilliseconds(100))
.Where(buffer => buffer.Count > 0)
.ObserveOn(RxApp.MainThreadScheduler);
observable.Subscribe(buffer =>
{
MessageText += string.Concat(buffer);
});
return Task.CompletedTask;
}
}
```

```charp
public partial class ChatViewModel:ReactiveObject
{
private const string _markdown = "**"
public ObservableCollection Messages { get; } = [];

[ReactiveCommand]
public async Task SendMessage(string message)
{
var modelMessage = new ModelMessage();
Messages.Add(modelMessage);
var textStream = GetTextStream(_markdown, delayMs: 5);
_= modelMessage.SubscribeAsync(textStream,cancellationToken:System.Threading.CancellationToken.None);
}

public static async IAsyncEnumerable GetTextStream(
string text,
int delayMs = 10,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (var ch in text)
{
cancellationToken.ThrowIfCancellationRequested();
yield return ch.ToString();
await Task.Delay(delayMs, cancellationToken);
}
}
````

** _markdown is a sample markdown code :
https://github.com/CommunityToolkit/Labs-Windows/blob/18946f6c6d5194e0dbc90e3c541cfc5284d07c09/components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml.cs#L25

### Steps to reproduce

```text
- Create a new winui3 project
- Paste the code
- Run/Debug
- Start a memory profile tool:process informer
```

### Expected behavior

Although the memory may temporarily spike to a higher value, it eventually returns to a normal level.

### Screenshots

_No response_

### Code Platform

- [ ] UWP
- [x] WinAppSDK / WinUI 3
- [ ] Web Assembly (WASM)
- [ ] Android
- [ ] iOS
- [ ] MacOS
- [ ] Linux / GTK

### Windows Build Number

- [ ] Windows 10 1809 (Build 17763)
- [ ] Windows 10 1903 (Build 18362)
- [ ] Windows 10 1909 (Build 18363)
- [x] Windows 10 2004 (Build 19041)
- [ ] Windows 10 20H2 (Build 19042)
- [ ] Windows 10 21H1 (Build 19043)
- [ ] Windows 11 21H2 (Build 22000)
- [ ] Other (specify)

### Other Windows Build number

_No response_

### App minimum and target SDK version

- [x] Windows 10, version 1809 (Build 17763)
- [ ] Windows 10, version 1903 (Build 18362)
- [ ] Windows 10, version 1909 (Build 18363)
- [ ] Windows 10, version 2004 (Build 19041)
- [ ] Other (specify)

### Other SDK version

_No response_

### Visual Studio Version

2022

### Visual Studio Build Number

17.14.18

### Device form factor

Desktop

### Additional context

_No response_

### Help us help you

Yes, I'd like to be assigned to work on this item.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.