Re: [dart-editor] Streams and memory management

315 views
Skip to first unread message

Seth Ladd

unread,
Nov 18, 2013, 8:32:25 PM11/18/13
to General Dart Discussion
(editor to BCC)
+ misc

Thanks for the question, Jeromy. I've routed over to our general discussion list.


On Mon, Nov 18, 2013 at 10:09 AM, Jeromy Streets <halve...@gmail.com> wrote:
My application creates and destroys objects continually, and in the process other objects subscribe to the streams. Do I need to cancel my Streams to prevent a memory leak, or is this handled, somehow, as objects go out of scope?

Thank you for any input!

--
You received this message because you are subscribed to the Google Groups "Dart Editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to editor+un...@dartlang.org.
For more options, visit https://groups.google.com/a/dartlang.org/groups/opt_out.

Robbie Boyd

unread,
Nov 18, 2013, 10:09:13 PM11/18/13
to mi...@dartlang.org
Hey Jeromy,

Can I ask what the result of your memory leaks are? I am having issues with TImers that don't seem to destroy themselves so the Dart VM crashes (aw snap!) and dart2js slowly chugs to a halt.

thanks,

Robbie

Lasse R.H. Nielsen

unread,
Nov 19, 2013, 3:54:50 AM11/19/13
to mi...@dartlang.org
On Tue, Nov 19, 2013 at 2:32 AM, Seth Ladd <seth...@google.com> wrote:
(editor to BCC)
+ misc

Thanks for the question, Jeromy. I've routed over to our general discussion list.


On Mon, Nov 18, 2013 at 10:09 AM, Jeromy Streets <halve...@gmail.com> wrote:
My application creates and destroys objects continually, and in the process other objects subscribe to the streams. Do I need to cancel my Streams to prevent a memory leak, or is this handled, somehow, as objects go out of scope?

The only general answer is: It depends.

It is always a good idea to cancel your stream subscriptions when you don't need them any more.
It allows the stream data provider to know that it should stop and clean up after itself.

If your data is coming from the outside, e.g., a socket or a file, then there is a hook from the operating system into the dart system that will keep the stream object alive, and will likely keep adding data to the stream and firing data events.

The same goes for any timers or scheduled tasks that may keep a stream alive. It's only if the stream is entirely self-contained, and you throw away the controller object that could put data into the stream, then it may be garbage collected eventually (and there certainly won't be any more data put into it).

I highly recommend canceling any stream subscription that you don't need any more. It is likely to continue without you.

/L
--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Florian Loitsch

unread,
Nov 19, 2013, 7:18:58 AM11/19/13
to General Dart Discussion
Small additional note for DOM nodes. If you are listening on a DOM stream and remove the nodes from the document (letting them garbage-collected) you don't need to cancel your subscription. *However* stream subscriptions keep a reference to their source, and developers need to make sure that the subscription doesn't keep the DOM node alive. There are two ways to achieve this:
- call cancel on the subscription (which severs the link between the subscription and the DOM node)
- let the subscription get garbage-collected.
Most of the time, just letting the subscription become garbage-collected is much easier, but if it is stored in a final field (for example) invoking 'cancel' is the only option.


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

Jeromy Streets

unread,
Nov 25, 2013, 10:51:50 AM11/25/13
to mi...@dartlang.org
Sorry about the late reply; took me a bit to find where the conversation got forwarded. I think I have my answer from the current replies (and thank you all very much for that!), though a bit more detail in case more input is to be had: I create objects from non-DOM classes that create broadcast streams. These streams are subscribed to by other objects from non-DOM classes. These objects (some for the data model, some to represent objects drawn on a canvas) are routinely created and destroyed. Sometimes it is the object generating the stream, sometimes it is the object listening to the stream. As it stands, now, I cancel all my non-permanent streams, however the question came up in code review about how necessary it was to cancel the stream subscriptions. It sounds like, at least in my case, I should cancel the subscriptions as they are not needed. I don't cancel subscriptions if both, the stream generator and the listener are destroyed, so if there is any cancelling I should be doing there, then let me know, though I assumed two objects going out of scope with a reference only to each other via a stream would get garbage collected correctly.

Thank you all, again, for your help!
-Jeromy Streets
Reply all
Reply to author
Forward
0 new messages