我正在使用angularjs-nvd3-directives来创建一个angularjs应用程序来渲染图表.
在使用Chrome开发者工具检查后,我检测到一些与图表相关的内存泄漏.当用户浏览包含图表的不同视图时,内存永远不会完全释放.
我已经在图形控制器上进行了一些清理:
$scope.$on('$destroy', function() {
d3.select( '#exampleId' ).remove();
d3.select( '#exampleId2' ).remove();
...
});
Run Code Online (Sandbox Code Playgroud)
并在routeChange事件:
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 = {};
}
});
});
Run Code Online (Sandbox Code Playgroud)
当我从我的应用程序中删除图表时,内存使用量总是回到初始值.
使用图表:
内部消除:

有没有其他方法来释放这些图表生成的内存?
jsfiddle来证明这个问题.