Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-5279

iOS: Repeatedly Scrolling a Table View Leaks Memory

    Details

      Description

      Problem

      When you repeatedly scroll up and down in a table view, memory is progressively leaked and is not reclaimed until a memory warning occurs. Do this for long enough, and the app will crash.

      When This Happens

      Based on experimentation, I see an inverse correlation between number of rows in the table and how quickly you need to scroll up and down before the table view will start leaking memory. For example, if you only have 50 rows, you have to scroll very quickly to get the problem to start. But if you increase that to 200 rows, it is much easier to see the leak. Further, with 200 rows, if you slowly scroll through, you won't see the problem.

      Sample Code

      Consider the following code, and download the attached iproducts.png to your resources directory:

      app.js

      var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
       
      var rows = [];
      for (var i = 0; i < 200; i++) {
          var row = Ti.UI.createTableViewRow();
          row.add(Ti.UI.createLabel({ text: 'Row ' + i, left: 70 }));
          row.add(Ti.UI.createImageView({ image: 'iproducts.png', left: 10 }));
          rows.push(row);
          row = null;
      }
      win.add(Ti.UI.createTableView({
          data: rows,
          bottom: 50
      }));
       
      var freeMemory = Ti.UI.createLabel({
          height: 50, bottom: 0
      });
      setInterval(function() {
          freeMemory.text = 'Free Memory: ' + Ti.Platform.availableMemory;
      }, 1000);
      win.add(freeMemory);
       
      win.open({
          modal: true,
          modalStyle: Ti.UI.iPhone.MODAL_PRESENTATION_FORMSHEET
      });
      

      Steps To Reproduce

      1. Drop the above in an app.js
      2. Download the attached iproducts.png to your Resources directory
      3. Run the app on an iPad 1; I have reproduced this in the simulator, but it is easier to see and reproduce on an actual device
      4. Slowly scroll through the whole table, making sure all of the rows are loaded.
      5. Look at the "Free Memory" label. You probably saw it decrease as the rows were loaded, and then stabilize. Slow scrolling through the table might cause this number to change, but it should stay pretty stable.
      6. Now rapidly scroll up and down in the table view. You should be going at a rate of around 2-3 flicks per second to go from top to bottom, and then 2-3 flicks to get back to the top in another second.
      7. Keep doing step 6, and watch "Free Memory". It might stay stable for a couple of seconds, but then you should see it go down by a megabyte or more per second. The OS will send a memory warning, and you'll jump back up to having lots of free memory. But it will continue to drain memory, and continue to hit memory warnings, until the OS forces the app to terminate.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                stephentramer Stephen Tramer
                Reporter:
                dtoth Dawson Toth
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-4468
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Integration