0

I have a background thread which is processing a lot of data.

The Background Thread uses IProgress in order to report back to the UserInterface (might not be best practice?):

public OrderBook UpdatedOrderBook;
internal async Task CompareQuotes(CancellationToken stop, IProgress<OrderBook> progress = null, IProgress<Exception> errorInterface = null)
{
    while (true)
    {
        UpdatedOrderbook = GetUpdatedOrderbook();
        /* Process Data */
        progress.Report(UpdatedOrderBook); // uncommenting this line solves memory leak
    }
} 

The userinterface starts the Background task like this:

public UserInterface()
{
    InitializeComponent();
    CancelTask = new CancellationTokenSource();
    var progressIndicator2 = new Progress<OrderBook>(ReportTriangularArbitrageProgress);
    var reportError = new Progress<Exception>(ReportError);

    Task botTask = Task.Run(() =>
    {
        Thread.CurrentThread.Name = "TriangularArbitrageCompareQuotes";
        Bot.CompareQuotes(stop: CancelTask.Token,progress: progressIndicator2, errorInterface: reportError);
    });
}

The UserInterface updates method is beeing called upon progress.Report. I set return in the first line to see if this will stop the leak but this does not seem to be the case.

void ReportTriangularArbitrageProgress(OrderBook updatedOrderBook)
{
    return;
}

As soon as I uncomment progress.Report(MyObject) from my background task, my memory stops to inflate. (But my ui wont update as well)

What is causing the Memory Leak and GC Pressure? How to prevent it from happening? Is there a better/proper way to update the UI?

julian bechtold
  • 1,875
  • 2
  • 19
  • 49
  • Increasing memory does not necessarily imply a memory leak. For example, `Progress` will post messages to the UI thread to invoke the callback. If you Report progress faster than the UI thread can process them, it might build up a backlog of progress messages on the message queue. Use a memory profiler to check what is holding on to the memory. – JonasH May 23 '22 at 08:14
  • Hello Jonas, thank you for the suggestion. The memory rises to > 1gb in a matter of seconds. VS won't even let me take a memory snapshot. I will try lowering the update Frequency or implement an ui update function which updates at a given framerate. – julian bechtold May 23 '22 at 08:47
  • That sounds like a good first step. Also, If the `OrderBook` object is large I would suggest posting a smaller object, like a double or a string, or both. I personally prefer to handle progress by polling instead of posting, i.e. have a shared field with the current progress-state, and let the UI thread poll this field, automatically throttling updates. But that pattern is not compatible with `Progress`. – JonasH May 23 '22 at 08:55
  • @JonasH that was how I did it previously. I had plenty of issues due to the OrderBook beeing refreshed while the ui update process is running. So I decided to push an instance of the OrderBook object to the ui rather than to fetch data – julian bechtold May 23 '22 at 09:40

0 Answers0