Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-10351] Android: Timers (setTimeout, setInterval) leak memory.

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2012-08-14T16:21:48.000+0000
Affected Version/sRelease 3.0.0
Fix Version/sSprint 2012-16 API, Release 2.1.2, Release 3.0.0
ComponentsAndroid
Labelscore, module_titanium, qe-review, qe-testadded
ReporterJosh Roesslein
AssigneeJosh Roesslein
Created2012-08-08T12:30:54.000+0000
Updated2012-09-11T09:49:40.000+0000

Description

Timers on Android currently never release the callback function once the timer has finished (fired once or was cancelled). A leak will be created since the callback function and any objects it retains will never get collected.

Timer leak example

function startTimer() {
  // 1 mb buffer
  var buffer = Ti.createBuffer({length: 1024 * 1024});

  setTimeout(function() {
    alert('Buffer: length = ' + buffer.length);
  }, 2000);
}

var win = Ti.UI.createWindow();

var startTimerButton = Ti.UI.createButton({title: 'Start timer'});
startTimerButton.addEventListener('click', startTimer);
win.add(startTimerButton);

win.open();
1. Run the test code and monitor the heap usage with DDMS. 2. Click the "Start timer" button and wait for an alert. 3. Dismiss the alert once it is shown and check the heap usage in DDMS. 4. Repeat steps 2-3 and watch for heap growth. Expected: No long term growth in heap size should happen. It should reach a equilibrium. Actual: The heap grows indefinitely eventually running out of memory.

Comments

  1. Josh Roesslein 2012-08-08

    Created [PR #2703](https://github.com/appcelerator/titanium_mobile/pull/2703) to resolve issue.
  2. Josh Roesslein 2012-08-14

    Created [PR #2748](https://github.com/appcelerator/titanium_mobile/pull/2748) to back port fix into 2.1.2 release.
  3. Olga Romero 2012-08-15

    Verified fix with: Titanium Studio, build: 2.1.1.201207271312 Titanium SDK: 2.1.2.v20120815081613 Devices: Droid 2.2.3 Nexus7 4.1 Mac osx 10.8 Mountain Lion

JSON Source