任务和垃圾收集存在哪些问题?

What gotchas exist with Tasks and Garbage Collection?(任务和垃圾收集存在哪些问题?)
本文介绍了任务和垃圾收集存在哪些问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用派生自任务并行库的 API 和类时,开发人员何时需要关注垃圾回收的影响?

When does a developer need to be concerned with the effects of garbage collection when using APIs and classes derived from the Task Parallel Library?

.NET 任务实例能否超出范围在运行期间?,似乎会给您一种安全感,您不必担心将任务保持在范围内.但是,问题似乎仅限于在 ThreadPool 上运行的任务,然后它们由 ThreadPool root.但是,如果我理解 这篇 MSDN 博文正确地,来自该 SO 问题的建议并不普遍适用,因为来自 TaskCompletionSource 的任务并非类似 rooted.

Can .NET Task instances go out of scope during run?, would seem to give a sense of security that you do not have to worry about keeping tasks in scope. However the question seems limited to Tasks running on the ThreadPool where they are then rooted by the ThreadPool. However, if I understand this MSDN blog post correctly, the advice from that SO question is not be generally applicable because Tasks from TaskCompletionSource are not similarly rooted.

直接使用 TaskCompletionSource 是唯一需要关注的时间吗?
但是,在使用 API 时,您不知道 Task 来自何处.如果提供的 Task 来自 TaskCompletionSource 或其他一些非根源,您是否需要担心存储对延续的引用?

Are direct use of TaskCompletionSource the only time of concern?
However, when consuming an API you do not know where the Task came from. Do you then need to worry about storing references to continuations in case the provided Task came from a TaskCompletionSource or some other non-rooted source?

这似乎很快变得不方便和复杂,因为需要考虑任务是否已植根(异步 I/O 任务是否植根?).我正在努力寻找有关该主题的大量信息,但它是一个足够流行的库遗漏或误解某事.

This seems to get inconvenient and complex quickly from needing to consider whether the Task is rooted or not (are Async I/O Tasks rooted?). I am struggling to find much for information on topic but it a popular enough library I feel I should not need to be reading decompiled source code to determine if I need to worry about race-conditions with the garbage collector, so I figure I must be missing or misunderstanding something.

推荐答案

当你有未完成的TaskCompletionSource,那么总有两种选择:

When you have uncompleted TaskCompletionSource, then there are always two options:

  1. 将来可能会完成该 TCS.这意味着某个东西持有对 TCS 的引用,这意味着它不能被 GCed.

  1. Something might complete that TCS in the future. That means that that something holds a reference to the TCS, which means it can't get GCed.

正常规则仍然适用于该内容,因此您可能需要担心保持其扎根.

Normal rules still apply to that something, so you might need to worry about keeping that rooted.

没有什么可以完成该 TCS.这意味着 TCS 及其任务可能很快就会被 GCed,但没有工作未完成的风险(因为没有工作).

Nothing will ever complete that TCS. That means the TCS and its Task will likely get GCed soon, but there is no risk of work not being done (because there is no work).

这篇关于任务和垃圾收集存在哪些问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)