Search code examples
c#garbage-collection

C# Create separate Garbage Collector per thread


I'm planning on creating a real-time audio editing and sequencing application in C# using mainly the cscore library for handling the audio on the backend, and then Avalonia for the frontend (using Avalonia.Markup.Declarative, although not sure that matters a huge amount). I'm expecting that the front-end is going to require occasional GC cleanup during normal operation/use, and would like to make sure that it won't affect the backend during GC - if the audio backend has to pause during GC there may be audible freezing or lag when this occurs. It's feasible for me to write code using pre-allocated memory or manual memory management for the backend, but it's not feasible for Avalonia, without modifying almost the entirety of its internal logic and massively changing how the I interact with the framework relative to the official docs (may not be possible, period - I've not really looked into it much).

Is there a way to create separate heaps/GC per thread, so that the backend threads can have their own separate GC not influenced by the UI, or would I have to create two separate applications in a server/client style and do some form of IPC between them. Alternatively, is there a way to mark backend code that requires no GC interaction to continue running during a GC clean-up, if I know for a fact it won't be affected. If so, how would be best to go about it, given the potentially large amounts of arbitrary data I'd like to transfer between them.


Solution

  • I don't think, that you can have two GCs for one app. But, you can make two apps:

    1. You app UI (appui.exe)
    2. You app Backend (appbackend.exe)

    For user you can make an shortcut on Desktop for your appui.exe. When the UI app is starting, it should check, do we have started appbackend.exe, or no. If no - start it and wait for loading.

    As a result, you will get two absolutely separated apps, with different GC. You can setup communications between apps using any network protocol, or you can take JetBrains.Rd