Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16450] Android: imageAsResized is leaking memory

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionFixed
Resolution Date2015-09-16T20:51:13.000+0000
Affected Version/sRelease 4.1.0
Fix Version/sRelease 5.1.0
ComponentsAndroid
LabelsSupportTeam
ReporterMauro Parra-Miranda
AssigneeAshraf Abu
Created2014-02-12T00:48:49.000+0000
Updated2015-10-15T18:16:46.000+0000

Description

Problem Description

When using the imageAsResize function in blob, it will start eventually to fail with a warning message about no more memory available.

Test Code

  var count = 0;
 
var win = Ti.UI.createWindow({
    backgroundImage : 'b1.png',
    layout : 'vertical'
});
var closeBtn = Ti.UI.createButton({
    title : 'close',
    top : '44dp',
});
var resizeBtn = Ti.UI.createButton({
    title : 'resize: ' + count,
    top : '20dp',
});
var imageView = Ti.UI.createImageView({
    width : '200dp',
    height : '200dp',
});
 
//--
win.add(closeBtn);
win.add(resizeBtn);
win.add(imageView);
 
//--
 
resizeBtn.addEventListener('click', function() {
    var f = Ti.Filesystem.getFile('b1.jpg');
    var blob = f.read();
    f = null;
    win.remove(imageView);
    imageView.view = null;
    imageView = null; 
    imageView = Ti.UI.createImageView({
    width : '200dp',
    height : '200dp',
	});
    imageView.image = blob.imageAsResized(960, 960);
    blob = null;
    win.add(imageView);
    count++;
    resizeBtn.title = 'resize: ' + count;
});
closeBtn.addEventListener('click', function() {
    win.close();
});
 
win.open();

Attachments

Check the attachments for b1.jpg

Steps to reproduce

1. Create mobile project (classic titanium) 2. Copy-Paste the code from the testcase into app.js 3. Copy bg1.jpg into Resources directory 4. Deploy into device 5. Click in resize until the image doesn't appear anymore.

Extra info

when the image doesn't appear anymore, you will see a message in the console saying:

[ERROR] :  TiBlob: (KrollRuntimeThread) [3227,231234] Unable to resize the image. Not enough memory: null
[ERROR] :  TiBlob: java.lang.OutOfMemoryError
[ERROR] :  TiBlob: 	at android.graphics.Bitmap.nativeCreate(Native Method)
[ERROR] :  TiBlob: 	at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
[ERROR] :  TiBlob: 	at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
[ERROR] :  TiBlob: 	at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
[ERROR] :  TiBlob: 	at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594)
[ERROR] :  TiBlob: 	at org.appcelerator.titanium.TiBlob.imageAsResized(TiBlob.java:543)
[ERROR] :  TiBlob: 	at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
[ERROR] :  TiBlob: 	at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:64)
[ERROR] :  TiBlob: 	at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:884)
[ERROR] :  TiBlob: 	at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1107)
[ERROR] :  TiBlob: 	at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:327)
[ERROR] :  TiBlob: 	at android.os.Handler.dispatchMessage(Handler.java:98)
[ERROR] :  TiBlob: 	at android.os.Looper.loop(Looper.java:136)
[ERROR] :  TiBlob: 	at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)
[INFO] :   I/dalvikvm-heap: Grow heap (frag case) to 182.277MB for 1474561

Q&A Link

http://developer.appcelerator.com/question/162431/android-memory-leaks

Attachments

FileDateSize
b1.jpg2014-02-12T00:48:49.000+00002037092

Comments

  1. Samuel East 2015-08-13

    I am also having this problem and news...
  2. Ashraf Abu 2015-09-16

    PR: https://github.com/appcelerator/titanium_mobile/pull/7197 In addition to imageAsResized, imageAsThumbnail, imageWithAlpha, imageWithRoundedCorner and imageWithTransparentBorder were also fixed to the memory from growing too large. Test code as above. Please test with other images and the other methods as well. The b1.jpg is quite large in general, hence for the other methods that do not resize, use a smaller image to test Alpha, Rounded Corner and TransparentBorder.
  3. Tim Poulsen 2015-09-17

    I'm glad this is fixed. It would be nice if you guys would implement support for camera.getParameters.setPictureSize() in the native camera module to help with memory problems like this. I created a module for this because the Titanium camera module resulted in too many memory errors. See https://github.com/skypanther/picatsize -- which is essentially TiCamera with a few modifications. Feel free to use whatever you want from it.
  4. Ashraf Abu 2015-09-17

    [~timpoulsen] Thanks. I'll try to take a look into your code.
  5. Lokesh Choudhary 2015-10-15

    Verified the fix. 1.Tested the imageAsResized & imageAsThumbnail with the attached image which is around 1.9MB. 2.Tested the imageWithAlpha, imageWithRoundedCorner and imageWithTransparentBorder` with a smaller image as these don't resize. Did not see any out of memory errors in any case with the fix. Closing. Environment: Appc Studio : 4.3.1.201509302304 Ti SDK : 5.1.0.v20151014101225 Ti CLI : 5.0.5 Alloy : 1.7.16 MAC Yosemite : 10.10.5 Appc NPM : 4.2.1-1 Appc CLI : 5.1.0-38 Node: v0.10.37 Android 4.2.2

JSON Source