Why does this use of getImageData leak memory

帅比萌擦擦* 提交于 2019-12-11 03:46:50

问题


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();
}

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?


回答1:


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();
}


来源:https://stackoverflow.com/questions/20645310/why-does-this-use-of-getimagedata-leak-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!