Question

I have created a background service that every second has to ask to the server if there are any new data. Using debug monitor what I notice is that the allocated memory is growing and growing till it says memory leak.

index.js

var intent = Titanium.Android.createServiceIntent({
    url: 'service.js'
  });
intent.putExtra('interval', 1000); // Needs to be milliseconds

Titanium.Android.startService(intent);

service.js

var URL = "localhost:8051";
Ti.API.info("checkForNotification: " + URL );

var xhr = Ti.Network.createHTTPClient({
     onload: function(e) {
             Ti.API.info('onload called, HTTP status = '+this.status); 
      },
     onerror: function(e) {
             Ti.API.info('error, HTTP status = '+this.status); 
     },
     timeout:5000  /* in milliseconds */
});

xhr.open('GET', URL);
xhr.send(); 

What I have realized so far is fault of xhr.send(), it does not get clean or maybe it's me doing something wrong. Can anyone suggest, how to avoid this memory leak?

Was it helpful?

Solution

Request timeout is 5 seconds and loop time is 1 second... So its obvious that it will send too many calls regardless its comes with the response or not. Even it comes before 5 second or not. So its better to keep the track of the calls and release the xhr variable manually. Or you can do it in another way by recursion, means send the call again when you receive the response of a first call and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top