如何在DARTVM应用程序中清理流?

发布于 2025-02-07 04:20:21 字数 361 浏览 2 评论 0原文

请注意,我正在问一个严格的 dart 应用程序 以任何方式都不关心颤动,dartvm是指飞镖虚拟机。

据我了解,DART通过流实现了DART的反应状态概念,处理流对象的生命周期的责任是给程序员的,在运行时,人们可以操纵流,因为他们认为根据其设计适合其设计,添加到流中;听它或处置它。

我的问题是,是否有必要在我的应用程序退出之前调用流的Dispose()方法?如果我这样做,我该如何实现?连接到VM状态并没有充分记录,并且使用processsignal听众无法便携,如果我不携带,GC是否处理这种情况?在这种情况下,最好的做法是什么?

Please note that I am asking about a strictly dart only application this does not concern flutter in any means, dartvm refers to the dart virtual machine.

As far as I understand Dart's idea of reactive state is implemented through streams, the responsibility of handling the lifetime of a stream object is given to the programmer, at runtime one could manipulate the stream as they see fit according to what works for their design by adding to the stream; listening to it or disposing it.

My question is this, Is it necessary that I need to call the dispose() method of a stream before my application quits? If I do, how do I go about accomplishing that? Hooking into the VM state isn't well documented and using ProcessSignal listeners is not portable, If I don't, does the GC handle this case? What's the best practice in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2025-02-14 04:20:21

飞镖流没有DISPOSE方法。因此,您无需称呼它。

但是只是为了提供更多细节...
飞镖流是许多东西。或更确切地说,流很简单,它们只是提供代码之间提供连接的一种方式,该代码提供了可消耗事件的事件和代码。调用收听之后,流对象不再是通信,事件和推回的一部分,直接在事件源之间进行(可能是streamController)和消费者(a StreamSubscription)。

活动提供商很多事情。

某些事件仅是通过执行代码来触发的。在这些之后,无需清理,它只是像其他所有内容一样飞镖对象,它们会随着程序而死,如果没有实时代码是指它们,则可以提前收集垃圾。

某些事件是由I/O操作在基础操作系统上触发的。这些程序结束时通常会清理这些,因为它们是通过飞镖运行时系统分配的,并且知道如何再次阻止它们。
一旦您不需要任何事件,就可以立即取消订阅仍然是一个好主意。这样,您就不会将文件打开太久,并阻止程序的另一部分覆盖该文件。

某些代码可能会分配其他资源,而不是由运行时管理,您应该格外小心说何时不再需要该资源。
您必须通过阅读流的文档来逐案确定。
对于通过DART分配的资源:FFI,您也可以使用antivalFinalizer为资源注册Dispose功能。

通常,如果您不需要流中的任何事件,则应始终取消订阅。那是你可以做的一件事。如果没有别的,它允许垃圾收集更早地收集东西。

Dart streams do not have a dispose method. Therefore you don't need to call it.

But just to give a little more detail ...
Dart streams are many things. Or rather, streams are pretty simple, they're just a way to provide a connection between code which provides events and code which consumes events. After calling listen, the stream object is no longer part of the communication, events and pushback goes directly between the event source (possibly a StreamController) and the consumer (a StreamSubscription).

Event providers are many things.

Some events are triggered just by code doing things. There is no need to clean up after those, it's just Dart objects like everything else, and they will die with the program, and can be garbage collected earlier if no live code refers to them.

Some events are triggered by I/O operations on the underlying operating system. Those will usually be cleaned up when the program ends, because they are allocated through the Dart runtime system, and it knows how to stop them again.
It's still a good idea to cancel the subscription as soon as you don't need any more events. That way, you won't keep a file open too long and prevent another part of the program from overwriting it.

Some code might allocate other resources, not managed by the runtime, and you should take extra care to say when that resource is no longer needed.
You'll have to figure that out on a case-by-case basis, by reading the documentation of the stream.
For resources allocated through dart:ffi, you can also use NativeFinalizer to register a dispose function for the resource.

Generally, you should always cancel the subscription if you don't need any more events from a stream. That's the one thing you can do. If nothing else, it allows garbage collection to collect things a little earlier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文