{"id":7556,"date":"2015-07-26T05:13:50","date_gmt":"2015-07-26T05:13:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/07\/26\/how-to-avoid-memory-leaks-using-angularjs-nvd3-directives-open-source-projects-novus-nvd3\/"},"modified":"2015-07-26T05:13:50","modified_gmt":"2015-07-26T05:13:50","slug":"how-to-avoid-memory-leaks-using-angularjs-nvd3-directives-open-source-projects-novus-nvd3","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/07\/26\/how-to-avoid-memory-leaks-using-angularjs-nvd3-directives-open-source-projects-novus-nvd3\/","title":{"rendered":"How to avoid memory leaks using angularjs-nvd3-directives-open source projects novus\/nvd3"},"content":{"rendered":"<p>I&#8217;m working on an angularjs application using angularjs-nvd3-directives to render charts.<\/p>\n<p>After a check with Chrome Developer Tools, I detected some memory leaks linked to the charts. When the user navigates through different views containing charts the memory is never fully released.<\/p>\n<p>I&#8217;m already doing some cleanup on the graphs controllers:<\/p>\n<pre><code>$scope.$on('$destroy', function() {\n  d3.select( '#exampleId' ).remove();\n  d3.select( '#exampleId2' ).remove();\n  ...\n});\n<\/code><\/pre>\n<p>And on the routeChange event:<\/p>\n<pre><code>myApp.run(function($rootScope, $templateCache) {\n  \/\/try to clear unused objects to avoid huge memory usage\n  $rootScope.$on('$routeChangeStart', function(event, next, current) {\n    if (typeof(current) !== 'undefined'){\n      \/\/destroy all d3 svg graph\n      d3.selectAll('svg').remove();\n      nv.charts = {};\n      nv.graphs = [];\n      nv.logs = {};\n    }\n  });\n});\n<\/code><\/pre>\n<p>When I remove the charts from my app, the memory usage always goes back to the initial value.<\/p>\n<p>With the graph: <img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/p0iP3.png\" \/> Whithout: <img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/rPVu7.png\" \/><\/p>\n<p>Is there any other way to release memory generated by those charts ?<\/p>\n<p>jsfiddle to demonstrate the issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on an angularjs application using angularjs-nvd3-directives to render charts. After a check with Chrome Developer Tools, I detected some memory leaks linked to the charts. When the user navigates through different views containing charts the memory is never fully released. I&#8217;m already doing some cleanup on the graphs controllers: $scope.$on(&#8216;$destroy&#8217;, function() { d3.select( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7556","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7556"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7556\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}