Javascript SVG内存泄漏
一个add,我在requestAnimationFrame循环中用900个内部元素重新创建SVGJavascript SVG内存泄漏,javascript,svg,memory-leaks,domparser,Javascript,Svg,Memory Leaks,Domparser,一个add,我在requestAnimationFrame循环中用900个内部元素重新创建SVG function SVGBuilder(){ this._parser = new DOMParser(); this._svg = null; } SVGBuilder.prototype.build = function(){ var string = '<svg width="300px" height="300px" xmlns="http:/
function SVGBuilder(){
this._parser = new DOMParser();
this._svg = null;
}
SVGBuilder.prototype.build = function(){
var string =
'<svg width="300px" height="300px" xmlns="http://www.w3.org/2000/svg">' +
Array.apply(null, {length:900}).map(function(_, i){
return '<rect class="rect" x="'+(i*10%300)+'" y="'+(i/30|0)*10+'" width="10" height="10" fill="#'+(Math.random()*1000000).toString(16).slice(-6)+'"/>'
}).join('') +
'</svg>'
if (this._svg) this._svg.parentNode.removeChild(this._svg);
this._svg = this.parse(string).documentElement;
document.body.appendChild(this._svg);
}
SVGBuilder.prototype.parse = function(string) {
return this._parser.parseFromString(string, 'image/svg+xml');
}
var SVG = new SVGBuilder();
function draw(){
SVG.build();
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
我知道重新绘制的意识很差,但我看不到哪里会出现高内存泄漏。我错过了什么?我不知道是什么原因导致了它,但chrome在该选项卡上的内存使用量增加到了2,5gig,所以至少我可以确认一次大规模内存泄漏。使用a而不是svg是一种选择吗?我可以使用canvas或其他svg算法重写它,而不会出现内存泄漏,但我需要解释为什么会出现这种泄漏。你的函数绘图中有一个不重要的尾部递归,永远不会倒转。请纠正我,但我认为它不是递归。requestAnimationFrame是异步函数