我想用Dart进行一些图像处理,同时使用requestAnimationFrame来不断更新我正在处理的图像。以下代码将泄漏内存,直到选项卡在Dartium崩溃。
import 'dart:html';
import 'dart:async';
final CanvasElement m_canvas = querySelector("#canvas");
void main() {
scheduleMicrotask(requestRedraw);
}
void requestRedraw() {
if(true)
{
window.requestAnimationFrame(draw);
}
}
void draw(num _) {
var context = m_canvas.context2D;
context.clearRect(0, 0, m_canvas.width, m_canvas.height);
var imageData = context.getImageData(0, 0, m_canvas.width, m_canvas.height);
requestRedraw();
}
在每次绘制调用完成后,imageData var显然超出了范围,但它保留的内存永远不会释放。注释掉这一行导致代码运行正常,以60 fps更新。这个内存泄漏是当前dart实现中的错误还是我做错了什么?
答案 0 :(得分:1)
这很奇怪但是当我在你的代码中添加一个print()
语句时,GC就会启动(至少在Dartium中)
int i = 0;
void draw(num _) {
var context = m_canvas.context2D;
context.clearRect(0, 0, m_canvas.width, m_canvas.height);
var imageData = context.getImageData(0, 0, m_canvas.width, m_canvas.height);
print(i++);
requestRedraw();
}