1

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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
HashAL78
  • 81
  • 1
  • 2
  • 8
  • 1
    How do you quit your program? – julemand101 Jun 13 '22 at 16:18
  • @julemand101 I am aware of two ways currently, one is by ctrl + c and the other is just normal execution flow. – HashAL78 Jun 13 '22 at 16:57
  • Well, I don't think you should care about garbage collection since your program are just being closed regardless (your OS will remove any used memory by the Dart process if you have just used Dart code to allocate the memory). But I think you should be more concerned if you want to make sure that all events are processed before the program stops. But if that is not important, then it does not really matter as long as the programs stops. See my answer here for a description of what makes a Dart program stop "naturally": https://stackoverflow.com/a/70670962/1953515 – julemand101 Jun 13 '22 at 17:05
  • Don't forget `exit` from `dart:io`. It's the fastest way to kill the process. You can also send `kill` events to individual isolates. Sending one to the main isolate will also end the program in the VM. – lrn Jun 13 '22 at 22:57

1 Answers1

2

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.

lrn
  • 64,680
  • 7
  • 105
  • 121