Flutter - Will BLoC stream instances cause memory leak when a widget is closed?












2














There are some scenarios where screens with their respective BLoCs are frequently created and closed. So I'm somewhat concerned about memory safety of the Streams instances created in this process, because it doesn't seem they are disposed somewhere or whether they are GC-ed. This clearly depends on the specific implementation of DART libraries and flutter. So if you know about their behavior, please let me know.



These are some scenarios I have encountered.




  1. Multi-tab browser-like application.

  2. Navigation through screens. (But it's not that harmful.)

  3. showDialog() senarios when there are BLoCs inside the dialog. This is a far more common senario. There could be a lot of dialog popping up frequently in an app.


I wonder if it is necessary to override dispose() function and explicitly close all streams in BLoCProvider. It seems existing tutorials didn't mention it.










share|improve this question



























    2














    There are some scenarios where screens with their respective BLoCs are frequently created and closed. So I'm somewhat concerned about memory safety of the Streams instances created in this process, because it doesn't seem they are disposed somewhere or whether they are GC-ed. This clearly depends on the specific implementation of DART libraries and flutter. So if you know about their behavior, please let me know.



    These are some scenarios I have encountered.




    1. Multi-tab browser-like application.

    2. Navigation through screens. (But it's not that harmful.)

    3. showDialog() senarios when there are BLoCs inside the dialog. This is a far more common senario. There could be a lot of dialog popping up frequently in an app.


    I wonder if it is necessary to override dispose() function and explicitly close all streams in BLoCProvider. It seems existing tutorials didn't mention it.










    share|improve this question

























      2












      2








      2







      There are some scenarios where screens with their respective BLoCs are frequently created and closed. So I'm somewhat concerned about memory safety of the Streams instances created in this process, because it doesn't seem they are disposed somewhere or whether they are GC-ed. This clearly depends on the specific implementation of DART libraries and flutter. So if you know about their behavior, please let me know.



      These are some scenarios I have encountered.




      1. Multi-tab browser-like application.

      2. Navigation through screens. (But it's not that harmful.)

      3. showDialog() senarios when there are BLoCs inside the dialog. This is a far more common senario. There could be a lot of dialog popping up frequently in an app.


      I wonder if it is necessary to override dispose() function and explicitly close all streams in BLoCProvider. It seems existing tutorials didn't mention it.










      share|improve this question













      There are some scenarios where screens with their respective BLoCs are frequently created and closed. So I'm somewhat concerned about memory safety of the Streams instances created in this process, because it doesn't seem they are disposed somewhere or whether they are GC-ed. This clearly depends on the specific implementation of DART libraries and flutter. So if you know about their behavior, please let me know.



      These are some scenarios I have encountered.




      1. Multi-tab browser-like application.

      2. Navigation through screens. (But it's not that harmful.)

      3. showDialog() senarios when there are BLoCs inside the dialog. This is a far more common senario. There could be a lot of dialog popping up frequently in an app.


      I wonder if it is necessary to override dispose() function and explicitly close all streams in BLoCProvider. It seems existing tutorials didn't mention it.







      dart flutter rxdart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 10:54









      First_Strike

      1128




      1128
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Streams will properly be cleaned as long as they aren't used anymore.
          The thing is, to simply removing the variable isn't enough to unsure it's unused. It could still run in background.



          You need to call Sink.close() so that it stops the associated StreamController, to ensure resources can later be freed by the GC.



          To do that, you have to use StatefulWidget.dispose method:



          abstract class MyBloc {
          Sink foo;
          Sink bar;
          }

          class MyWiget extends StatefulWidget {
          @override
          _MyWigetState createState() => _MyWigetState();
          }

          class _MyWigetState extends State<MyWiget> {
          MyBloc bloc;

          @override
          void dispose() {
          bloc.bar.close();
          bloc.foo.close();
          super.dispose();
          }

          @override
          Widget build(BuildContext context) {
          // ...
          }
          }





          share|improve this answer





















          • Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
            – stt106
            Nov 20 '18 at 11:10










          • @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
            – Rémi Rousselet
            Nov 20 '18 at 11:13










          • will chat to you on slack.
            – stt106
            Nov 20 '18 at 11:14










          • Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
            – First_Strike
            Nov 20 '18 at 11:57












          • You need both an Inheritedwidget and a StatefulWidget
            – Rémi Rousselet
            Nov 20 '18 at 12:05











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391431%2fflutter-will-bloc-stream-instances-cause-memory-leak-when-a-widget-is-closed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Streams will properly be cleaned as long as they aren't used anymore.
          The thing is, to simply removing the variable isn't enough to unsure it's unused. It could still run in background.



          You need to call Sink.close() so that it stops the associated StreamController, to ensure resources can later be freed by the GC.



          To do that, you have to use StatefulWidget.dispose method:



          abstract class MyBloc {
          Sink foo;
          Sink bar;
          }

          class MyWiget extends StatefulWidget {
          @override
          _MyWigetState createState() => _MyWigetState();
          }

          class _MyWigetState extends State<MyWiget> {
          MyBloc bloc;

          @override
          void dispose() {
          bloc.bar.close();
          bloc.foo.close();
          super.dispose();
          }

          @override
          Widget build(BuildContext context) {
          // ...
          }
          }





          share|improve this answer





















          • Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
            – stt106
            Nov 20 '18 at 11:10










          • @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
            – Rémi Rousselet
            Nov 20 '18 at 11:13










          • will chat to you on slack.
            – stt106
            Nov 20 '18 at 11:14










          • Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
            – First_Strike
            Nov 20 '18 at 11:57












          • You need both an Inheritedwidget and a StatefulWidget
            – Rémi Rousselet
            Nov 20 '18 at 12:05
















          2














          Streams will properly be cleaned as long as they aren't used anymore.
          The thing is, to simply removing the variable isn't enough to unsure it's unused. It could still run in background.



          You need to call Sink.close() so that it stops the associated StreamController, to ensure resources can later be freed by the GC.



          To do that, you have to use StatefulWidget.dispose method:



          abstract class MyBloc {
          Sink foo;
          Sink bar;
          }

          class MyWiget extends StatefulWidget {
          @override
          _MyWigetState createState() => _MyWigetState();
          }

          class _MyWigetState extends State<MyWiget> {
          MyBloc bloc;

          @override
          void dispose() {
          bloc.bar.close();
          bloc.foo.close();
          super.dispose();
          }

          @override
          Widget build(BuildContext context) {
          // ...
          }
          }





          share|improve this answer





















          • Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
            – stt106
            Nov 20 '18 at 11:10










          • @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
            – Rémi Rousselet
            Nov 20 '18 at 11:13










          • will chat to you on slack.
            – stt106
            Nov 20 '18 at 11:14










          • Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
            – First_Strike
            Nov 20 '18 at 11:57












          • You need both an Inheritedwidget and a StatefulWidget
            – Rémi Rousselet
            Nov 20 '18 at 12:05














          2












          2








          2






          Streams will properly be cleaned as long as they aren't used anymore.
          The thing is, to simply removing the variable isn't enough to unsure it's unused. It could still run in background.



          You need to call Sink.close() so that it stops the associated StreamController, to ensure resources can later be freed by the GC.



          To do that, you have to use StatefulWidget.dispose method:



          abstract class MyBloc {
          Sink foo;
          Sink bar;
          }

          class MyWiget extends StatefulWidget {
          @override
          _MyWigetState createState() => _MyWigetState();
          }

          class _MyWigetState extends State<MyWiget> {
          MyBloc bloc;

          @override
          void dispose() {
          bloc.bar.close();
          bloc.foo.close();
          super.dispose();
          }

          @override
          Widget build(BuildContext context) {
          // ...
          }
          }





          share|improve this answer












          Streams will properly be cleaned as long as they aren't used anymore.
          The thing is, to simply removing the variable isn't enough to unsure it's unused. It could still run in background.



          You need to call Sink.close() so that it stops the associated StreamController, to ensure resources can later be freed by the GC.



          To do that, you have to use StatefulWidget.dispose method:



          abstract class MyBloc {
          Sink foo;
          Sink bar;
          }

          class MyWiget extends StatefulWidget {
          @override
          _MyWigetState createState() => _MyWigetState();
          }

          class _MyWigetState extends State<MyWiget> {
          MyBloc bloc;

          @override
          void dispose() {
          bloc.bar.close();
          bloc.foo.close();
          super.dispose();
          }

          @override
          Widget build(BuildContext context) {
          // ...
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 11:03









          Rémi Rousselet

          24.8k24682




          24.8k24682












          • Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
            – stt106
            Nov 20 '18 at 11:10










          • @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
            – Rémi Rousselet
            Nov 20 '18 at 11:13










          • will chat to you on slack.
            – stt106
            Nov 20 '18 at 11:14










          • Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
            – First_Strike
            Nov 20 '18 at 11:57












          • You need both an Inheritedwidget and a StatefulWidget
            – Rémi Rousselet
            Nov 20 '18 at 12:05


















          • Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
            – stt106
            Nov 20 '18 at 11:10










          • @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
            – Rémi Rousselet
            Nov 20 '18 at 11:13










          • will chat to you on slack.
            – stt106
            Nov 20 '18 at 11:14










          • Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
            – First_Strike
            Nov 20 '18 at 11:57












          • You need both an Inheritedwidget and a StatefulWidget
            – Rémi Rousselet
            Nov 20 '18 at 12:05
















          Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
          – stt106
          Nov 20 '18 at 11:10




          Do you think it's always better to use access a bloc via an interface than using a provider which effectively uses an inherited widget?
          – stt106
          Nov 20 '18 at 11:10












          @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
          – Rémi Rousselet
          Nov 20 '18 at 11:13




          @stt106 What do you mean? An interface doesn't replace the provider, nor does the provider replace the interface
          – Rémi Rousselet
          Nov 20 '18 at 11:13












          will chat to you on slack.
          – stt106
          Nov 20 '18 at 11:14




          will chat to you on slack.
          – stt106
          Nov 20 '18 at 11:14












          Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
          – First_Strike
          Nov 20 '18 at 11:57






          Oh, thanks. So it seems we need to close the Streams in the dispose() function. But there are some other problems with this solution. InheritedWidget has their own lifecycle. I'm not so sure about this, but is it likely that either user or flutter engine triggers an unintended disposal of InheritedWidget? Because I've seen in some tutorials that BLoCs becomes shared and InheritedWidget are created and re-assigned everywhere.
          – First_Strike
          Nov 20 '18 at 11:57














          You need both an Inheritedwidget and a StatefulWidget
          – Rémi Rousselet
          Nov 20 '18 at 12:05




          You need both an Inheritedwidget and a StatefulWidget
          – Rémi Rousselet
          Nov 20 '18 at 12:05


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391431%2fflutter-will-bloc-stream-instances-cause-memory-leak-when-a-widget-is-closed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?