Creating a execution queue by using Task.ContinueWith?

后端 未结 3 804
旧巷少年郎
旧巷少年郎 2021-02-04 16:32

I have several actions that I want to execute in the background, but they have to be executed synchronously one after the other.

I was wondering if it\'s a good idea to

3条回答
  •  忘了有多久
    2021-02-04 17:37

    There is one flaw with this, which I recently discovered myself because I am also using this method of ensuring tasks execute sequentially.

    In my application I had thousands of instances of these mini-queues and quickly discovered I was having memory issues. Since these queues were often idle I was holding onto the last completed task object for a long time and preventing garbage collection. Since the result object of the last completed task was often over 85,000 bytes it was allocated to Large Object Heap (which does not perform compaction during garbage collection). This resulted in fragmentation of the LOH and the process continuously growing in size.

    As a hack to avoid this, you can schedule a no-op task right after the real one within your lock. For a real solution, I will need to move to a different method of controlling the scheduling.

提交回复
热议问题