Memory leak on toImage() function

If you call something like the following function, the available memory will decrease every time by a significant amount (because of the high definition pictures captured by iPhone 4 camera).

function cameraCallback(e){
    var imageBlob =;
    var imageHolder = Ti.UI.createImageView({image:imageBlob});
    // more logic such as re-scaling the image
    / ...
    var newImageBlob = imageHolder.toImage();
    // Do something with newImageBlob

I tested each line where the memory leak occurs, and it was the line that calls imageHolder.toImage(). If this function is called multiple times, the app eventually crashes because of the out of memory error.

Anyone has any solution to this?
Is this a known bug in Titanium Mobile?

3 Answers

  • Did you try win.remove(imageHolder) after you're done with it?

  • I have experienced the same leak.
    I called toImage, built an imageView, animated it, and removed it. Still leaked.

  • Avoid using ".toImage" on webviews and hierarchies with webviews. It only takes one ".toImage" call on a webview to cause a catastrophic memory leak.

    In my case, I had an easy work around. Since I was using a RGraph, I could just call ".evalJS" on the webview with javascript that returned a ".toDataURL" against the canvas. With that data, I was able to populate an imageView.