Problems with memory and a large json string on Android

You must Login before you can answer or comment on any questions.

Hi, In my app i load huge data from a web service. First time every thing is okay. I get this logs in the console:

[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 4.759MB for 924185-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 5.637MB for 924185-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 6.225MB for 616128-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 6.518MB for 1232240-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 8.281MB for 2464464-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 9.457MB for 1232240-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 8.867MB for 1846398-byte allocation
My tableview is filled with the data and every thing is okay. Now when i close my app and start it again, an error will occur:
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 11.534MB for 924185-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 12.388MB for 924185-byte allocation
[INFO][dalvikvm-heap(  492)] Grow heap (frag case) to 12.976MB for 616128-byte allocation
[INFO][dalvikvm-heap(  492)] Forcing collection of SoftReferences for 1232240-byte allocation
[ERROR][dalvikvm-heap(  492)] Out of memory on a 1232240-byte allocation.
How can i release the memory. I only have this problem on Android. I think I'll get the error on this line:
var data = JSON.parse(this.responseText);
My app is closed by backbutton and the property exitOnClose: true

I tried to release my tableview in the close-event of the window with

this.listWindow.addEventListener('close', function(e) {
        datasetListView = null;
    });
but that doesn't help.

— asked 8 months ago by Alexander Stark
1 Comment
  • I just ran into the same case (handling large json results after exiting and reopening an Android app that was closed via the android back button on a window with exitOnClose: true) and am receiving the same out of memory error. Have you discovered anything?

    — commented 7 months ago by Justin Toth

2 Answers

I have a simple sample app which shows the problem. app.js:

// This is a single context application with mutliple windows in a stack
(function() {
    var Window = require('ui/ApplicationWindow');
    new Window().open();
})();
ApplicationWindow.js:
//Application Window Component Constructor
function ApplicationWindow() {
 
    var username  = 'xxx';
        var password  = 'xxx';
        var serverURL = 'http://xxx';
 
    //create component instance
    var self = Ti.UI.createWindow({
        backgroundColor:'#ffffff'
    });
 
    // release components on close
    self.addEventListener('close', function(e) {
        Ti.API.info('close event fired');
        self.remove(tableView);
        tableView = null;
    });
 
    //construct UI
    var tableView = new Ti.UI.createTableView({
        scrollable: true
    });
    self.add(tableView);
 
    var json;
    var xhr = Ti.Network.createHTTPClient({});
    xhr.onload = function(e) {
        Ti.API.info('data received');
        var data = JSON.parse(this.responseText);
        Ti.API.info('data parsed with length '+ data.length);
        if (data) {
 
        };
        data = null;
    };
    xhr.open("GET", serverURL);
    var authstr = 'Basic ' + Titanium.Utils.base64encode(username + ':' + password);
    xhr.setRequestHeader('Authorization', authstr);
    xhr.send();
 
    return self;
};
 
//make constructor function the public component interface
module.exports = ApplicationWindow;
What i do is to send a HTTP-request to get an JSON-String. With every response i get the memory usage grows. First response:
[INFO][TiAPI   (  372)] data received
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 3.979MB for 964805-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 4.880MB for 964805-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 5.494MB for 643208-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 5.800MB for 1286400-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 7.640MB for 2572784-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 8.867MB for 1286400-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 8.251MB for 1927492-byte allocation
[INFO][TiAPI   (  372)] data parsed with length 315
Now I close the app with Back-Button. Second response:
[INFO][TiAPI   (  372)] data received
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 9.646MB for 2572784-byte allocation
[INFO][dalvikvm-heap(  372)] Grow heap (frag case) to 10.257MB for 1927492-byte allocation
[INFO][TiAPI   (  372)] data parsed with length 315
and so on

Hi Alexander, try increasing the memory size of you emulator...as with new installation it keep adding some resources and show problem in installing.... use AVD manager to increase the size of emulator memory..

— answered 8 months ago by Ashish Nigam
answer permalink
8 Comments
  • delete the app from emulator and then install again... that will work. for any other testing if possible then check on Device as well for real time scenario... Device should not report any memory problem in 2nd or 3rd time installation..

    — commented 8 months ago by Ashish Nigam

  • In real time i wouldn't reinstall my application before every using. When i close my emulator and restart the app everything is okay. It's also okay wenn i restart my app from Titanium Studio. I have the problems only when i close my app in the emulator and restart it from my dashboard in the emulator

    — commented 8 months ago by Alexander Stark

  • i know, thats why i am saying... try the same thing in device and there should not be any problem... you can do same thing in device also... rather than emulator... in emulator size of SD CARD and other memory size matters.. so you can try AVD manager to increase the size of them.

    — commented 8 months ago by Ashish Nigam

  • Show 5 more comments

Your Answer

Think you can help? Login to answer this question!