Memory usage is gradually increasing with javascript websockets.Why?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a web app using javascript websockets to fetch and render realtime data.The problem is once the websocket connection is established,the memory usage of my browser gradually increases in MBs.
Once I close the tab,the memory usage drops by the same amount as it increased.
I am rendering the data received through websockets to google chart as well and the data is received every 2 seconds triggering a re-render of the chart with new data.Could that be the issue?Or are websockets causing the rise in memory usage?










share|improve this question





























    -1















    I have a web app using javascript websockets to fetch and render realtime data.The problem is once the websocket connection is established,the memory usage of my browser gradually increases in MBs.
    Once I close the tab,the memory usage drops by the same amount as it increased.
    I am rendering the data received through websockets to google chart as well and the data is received every 2 seconds triggering a re-render of the chart with new data.Could that be the issue?Or are websockets causing the rise in memory usage?










    share|improve this question

























      -1












      -1








      -1








      I have a web app using javascript websockets to fetch and render realtime data.The problem is once the websocket connection is established,the memory usage of my browser gradually increases in MBs.
      Once I close the tab,the memory usage drops by the same amount as it increased.
      I am rendering the data received through websockets to google chart as well and the data is received every 2 seconds triggering a re-render of the chart with new data.Could that be the issue?Or are websockets causing the rise in memory usage?










      share|improve this question














      I have a web app using javascript websockets to fetch and render realtime data.The problem is once the websocket connection is established,the memory usage of my browser gradually increases in MBs.
      Once I close the tab,the memory usage drops by the same amount as it increased.
      I am rendering the data received through websockets to google chart as well and the data is received every 2 seconds triggering a re-render of the chart with new data.Could that be the issue?Or are websockets causing the rise in memory usage?







      javascript performance google-chrome websocket






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 29 '18 at 5:23









      midhun lcmidhun lc

      14




      14
























          2 Answers
          2






          active

          oldest

          votes


















          0














          There's no way we can have any concrete idea what is causing this problem without knowing a lot more about your app and seeing your code and probably instrumenting it as described below.



          It's not a generic problem with webSockets. webSockets can be open and receiving data for years without causing memory to increase.



          These problems are solved by methods like these:




          1. Code inspection (find a place where data is accumulated indefinitely)

          2. Finding articles about people having similar problems and looking at their solutions to see if they apply to you

          3. Instruction to take heap snapshots, comparing them with previous ones and seeing what kind of objects are accumulating and then using that knowledge to find the source of the data accumulation.


          See How to Record Heap Snapshots in Chrome for a very detailed description of doing memory use debugging. There's not really anything more specific than that we can tell you based on the limit info we have on your specific case.



          You could probably eliminate webSockets from the equation entirely by not connecting your webSocket and then just simulate getting data from the webSocket by running setInterval() and rerendering your chart over and over. That might at least eliminate some of your code as a suspect.



          FYI, here are a couple other articles to look at:



          Memory leak using google charts with ajax



          Memory leak in google line charts



          Quick tips for squeezing memory leaks out of Google's Javascript charts with live (continuous) data/streams



          Profiling memory usage in Chrome






          share|improve this answer

































            0














            I figured out the problem.
            Thanks @jFriend00 for the guidance.



            I assumed the problem to be with websocket after some lazy reading of blogs/posts/articles and posted the question here after that.



            I re-checked my code and found that the issue was with re-rendering charts every second.I was creating a new instance of the chart for rendering every second.I was hoping GC would do the job of clearing up the old chart but it wasn't so.



            I modified my code to clear the existing chart and render the same with new data and Voila! my memory consumption is back to normal.






            share|improve this answer
























              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%2f53532365%2fmemory-usage-is-gradually-increasing-with-javascript-websockets-why%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              There's no way we can have any concrete idea what is causing this problem without knowing a lot more about your app and seeing your code and probably instrumenting it as described below.



              It's not a generic problem with webSockets. webSockets can be open and receiving data for years without causing memory to increase.



              These problems are solved by methods like these:




              1. Code inspection (find a place where data is accumulated indefinitely)

              2. Finding articles about people having similar problems and looking at their solutions to see if they apply to you

              3. Instruction to take heap snapshots, comparing them with previous ones and seeing what kind of objects are accumulating and then using that knowledge to find the source of the data accumulation.


              See How to Record Heap Snapshots in Chrome for a very detailed description of doing memory use debugging. There's not really anything more specific than that we can tell you based on the limit info we have on your specific case.



              You could probably eliminate webSockets from the equation entirely by not connecting your webSocket and then just simulate getting data from the webSocket by running setInterval() and rerendering your chart over and over. That might at least eliminate some of your code as a suspect.



              FYI, here are a couple other articles to look at:



              Memory leak using google charts with ajax



              Memory leak in google line charts



              Quick tips for squeezing memory leaks out of Google's Javascript charts with live (continuous) data/streams



              Profiling memory usage in Chrome






              share|improve this answer






























                0














                There's no way we can have any concrete idea what is causing this problem without knowing a lot more about your app and seeing your code and probably instrumenting it as described below.



                It's not a generic problem with webSockets. webSockets can be open and receiving data for years without causing memory to increase.



                These problems are solved by methods like these:




                1. Code inspection (find a place where data is accumulated indefinitely)

                2. Finding articles about people having similar problems and looking at their solutions to see if they apply to you

                3. Instruction to take heap snapshots, comparing them with previous ones and seeing what kind of objects are accumulating and then using that knowledge to find the source of the data accumulation.


                See How to Record Heap Snapshots in Chrome for a very detailed description of doing memory use debugging. There's not really anything more specific than that we can tell you based on the limit info we have on your specific case.



                You could probably eliminate webSockets from the equation entirely by not connecting your webSocket and then just simulate getting data from the webSocket by running setInterval() and rerendering your chart over and over. That might at least eliminate some of your code as a suspect.



                FYI, here are a couple other articles to look at:



                Memory leak using google charts with ajax



                Memory leak in google line charts



                Quick tips for squeezing memory leaks out of Google's Javascript charts with live (continuous) data/streams



                Profiling memory usage in Chrome






                share|improve this answer




























                  0












                  0








                  0







                  There's no way we can have any concrete idea what is causing this problem without knowing a lot more about your app and seeing your code and probably instrumenting it as described below.



                  It's not a generic problem with webSockets. webSockets can be open and receiving data for years without causing memory to increase.



                  These problems are solved by methods like these:




                  1. Code inspection (find a place where data is accumulated indefinitely)

                  2. Finding articles about people having similar problems and looking at their solutions to see if they apply to you

                  3. Instruction to take heap snapshots, comparing them with previous ones and seeing what kind of objects are accumulating and then using that knowledge to find the source of the data accumulation.


                  See How to Record Heap Snapshots in Chrome for a very detailed description of doing memory use debugging. There's not really anything more specific than that we can tell you based on the limit info we have on your specific case.



                  You could probably eliminate webSockets from the equation entirely by not connecting your webSocket and then just simulate getting data from the webSocket by running setInterval() and rerendering your chart over and over. That might at least eliminate some of your code as a suspect.



                  FYI, here are a couple other articles to look at:



                  Memory leak using google charts with ajax



                  Memory leak in google line charts



                  Quick tips for squeezing memory leaks out of Google's Javascript charts with live (continuous) data/streams



                  Profiling memory usage in Chrome






                  share|improve this answer















                  There's no way we can have any concrete idea what is causing this problem without knowing a lot more about your app and seeing your code and probably instrumenting it as described below.



                  It's not a generic problem with webSockets. webSockets can be open and receiving data for years without causing memory to increase.



                  These problems are solved by methods like these:




                  1. Code inspection (find a place where data is accumulated indefinitely)

                  2. Finding articles about people having similar problems and looking at their solutions to see if they apply to you

                  3. Instruction to take heap snapshots, comparing them with previous ones and seeing what kind of objects are accumulating and then using that knowledge to find the source of the data accumulation.


                  See How to Record Heap Snapshots in Chrome for a very detailed description of doing memory use debugging. There's not really anything more specific than that we can tell you based on the limit info we have on your specific case.



                  You could probably eliminate webSockets from the equation entirely by not connecting your webSocket and then just simulate getting data from the webSocket by running setInterval() and rerendering your chart over and over. That might at least eliminate some of your code as a suspect.



                  FYI, here are a couple other articles to look at:



                  Memory leak using google charts with ajax



                  Memory leak in google line charts



                  Quick tips for squeezing memory leaks out of Google's Javascript charts with live (continuous) data/streams



                  Profiling memory usage in Chrome







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 29 '18 at 5:52

























                  answered Nov 29 '18 at 5:46









                  jfriend00jfriend00

                  442k55581624




                  442k55581624

























                      0














                      I figured out the problem.
                      Thanks @jFriend00 for the guidance.



                      I assumed the problem to be with websocket after some lazy reading of blogs/posts/articles and posted the question here after that.



                      I re-checked my code and found that the issue was with re-rendering charts every second.I was creating a new instance of the chart for rendering every second.I was hoping GC would do the job of clearing up the old chart but it wasn't so.



                      I modified my code to clear the existing chart and render the same with new data and Voila! my memory consumption is back to normal.






                      share|improve this answer




























                        0














                        I figured out the problem.
                        Thanks @jFriend00 for the guidance.



                        I assumed the problem to be with websocket after some lazy reading of blogs/posts/articles and posted the question here after that.



                        I re-checked my code and found that the issue was with re-rendering charts every second.I was creating a new instance of the chart for rendering every second.I was hoping GC would do the job of clearing up the old chart but it wasn't so.



                        I modified my code to clear the existing chart and render the same with new data and Voila! my memory consumption is back to normal.






                        share|improve this answer


























                          0












                          0








                          0







                          I figured out the problem.
                          Thanks @jFriend00 for the guidance.



                          I assumed the problem to be with websocket after some lazy reading of blogs/posts/articles and posted the question here after that.



                          I re-checked my code and found that the issue was with re-rendering charts every second.I was creating a new instance of the chart for rendering every second.I was hoping GC would do the job of clearing up the old chart but it wasn't so.



                          I modified my code to clear the existing chart and render the same with new data and Voila! my memory consumption is back to normal.






                          share|improve this answer













                          I figured out the problem.
                          Thanks @jFriend00 for the guidance.



                          I assumed the problem to be with websocket after some lazy reading of blogs/posts/articles and posted the question here after that.



                          I re-checked my code and found that the issue was with re-rendering charts every second.I was creating a new instance of the chart for rendering every second.I was hoping GC would do the job of clearing up the old chart but it wasn't so.



                          I modified my code to clear the existing chart and render the same with new data and Voila! my memory consumption is back to normal.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 29 '18 at 8:23









                          midhun lcmidhun lc

                          14




                          14






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532365%2fmemory-usage-is-gradually-increasing-with-javascript-websockets-why%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

                              Lallio

                              Unable to find Lightning Node

                              Futebolista