【发布时间】:2023-03-18 00:30:01
【问题描述】:
我正在开发一个使用 angularjs-nvd3-directives 来呈现图表的 angularjs 应用程序。
在使用 Chrome 开发者工具检查后,我检测到一些与图表相关的内存泄漏。当用户浏览包含图表的不同视图时,内存永远不会完全释放。
我已经对图形控制器进行了一些清理工作:
$scope.$on('$destroy', function() {
d3.select( '#exampleId' ).remove();
d3.select( '#exampleId2' ).remove();
...
});
在 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 = {};
}
});
});
当我从我的应用程序中删除图表时,内存使用总是回到初始值。
用图表: 没有:
还有其他方法可以释放这些图表产生的内存吗?
jsfiddle 演示问题。
【问题讨论】:
-
我没有答案,但我也渴望找到一种方法来破坏状态/路线变化的图表。这个问题在这里讨论:github.com/novus/nvd3/pull/396
-
嗨!您使用哪个版本的 nvd3?
-
@Artemis v1.1.15-beta
标签: angularjs d3.js memory-leaks nvd3.js