为什么这样使用getImageData泄漏内存 [英] Why does this use of getImageData leak memory

查看:272
本文介绍了为什么这样使用getImageData泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Dart做一些图像处理,而使用requestAnimationFrame来不断更新我正在处理的图像。以下代码将在Dartium中崩溃之前泄漏内存。

I would like to do some image processing with Dart while using requestAnimationFrame to continually update the image I am processing. The following code will leak memory until the tab crashes in 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更新。

The imageData var is clearly going out of scope with after each draw call completes yet the memory that it retains is never released. Commenting out this line results in the code running fine, updating at 60 fps. Is this memory leak a bug in the current dart implementation or am I doing something wrong?

推荐答案

这是很奇怪的,但是当我做错了?添加一个 print()语句到您的代码GC(至少在Dartium)

It is very weird but when I add a print() statement to your code the GC kicks in (at least in 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();
}

这篇关于为什么这样使用getImageData泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆