如何使用angularjs-nvd3-directives避免内存泄漏

我正在使用 angularjs-nvd3命令来渲染图表.

在使用Chrome Developer Tools检查后,我检测到与图表相关联的一些内存泄漏.当用户浏览包含图表的不同视图时,内存从未完全释放.

我已经在图形控制器上进行了一些清理:

$scope.$on('$destroy',function() {
  d3.select( '#exampleId' ).remove();
  d3.select( '#exampleId2' ).remove();
  ...
});

并在路线更改事件:

myApp.run(function($rootScope,$templateCache) {
  //try to clear unused objects to avoid huge memory usage
  $rootScope.$on('$routeChangeStart',function(event,next,current) {
    if (typeof(current) !== 'undefined'){
      //destroy all d3 svg graph
      d3.selectAll('svg').remove();
      nv.charts = {};
      nv.graphs = [];
      nv.logs = {};
    }
  });
});

当我从我的应用程序中删除图表时,内存使用总是返回到初始值.

用图表:

内部消除:

是否有其他方式释放由这些图表生成的内存?

jsfiddle来展示这个问题.

你可能会忘记删除窗口调整大小的监听器.
angularapp.run(function($rootScope) {
  $rootScope.$on('$routeChangeStart',current) {
    if (typeof(current) !== 'undefined'){
        //destroy d3 stuff 
        window.nv.charts = {};
        window.nv.graphs = [];
        window.nv.logs = {};

        // and remove listeers for onresize. 
        window.onresize = null;
    }
  });
});

还可以尝试删除整个svg元素,但似乎不是最好的方法.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...