Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

httpclient memory leak

All,

I have code similar to this:

var downloader = Ti.Network.createHTTPClient({timeout:10000});
downloader.onload = function(){
    //Save my file
    //Handle errors first, this is only a sample code
    saveFile(this.folderName, this.fileName, this.responseData);
}

Basically this code exists in a .js file by itself and is included on all other .js files that need the client response.

The code below is basically what i have in the individual .js files to use the downloader/xhr

//custom properties you can access using this.xxx
//inside you onload event manager
downloader.fileName = 'foo.jpg';
downloader.folderName = 'images';
//open & send 
downloader.open('GET', fileURL);
downloader.send();

The issue that is happening is whenever i issue the "downloader.send()" function I get a memory leak in the Instruments tool, and its always related the the NSURL or other other http/url related items (away from the system now so I can't be much more specific).

I've determined it is directly related to the httpclient because in my code if I read the response data, that I would normally get from the httpclient, from a file I don't have any memory leaks and all is happy, but as soon as I introduce the httpclient I start leaking and the leaks build as the data is refreshed.

I have tried with timeouts of different lengths, tried using .onload and .onreadystatechange, tried synchronous connections, tried setTimeout in the onload and onreadystatechange functions and a few other things that aren't coming to mind right now.

Note: I always get the data(json) so that's not the issue, I am just trying to get rid of the memory leak.

Can anyone shed any light on this or provide any other thoughts or ideas?

Thanks!

— asked November 19th 2010 by Chris Campbell
  • httpclient
  • leak
  • memory
  • xhr
0 Comments

2 Answers

  • Hello,

    I'm getting the same issue, which seems pretty big to me, I hope Appcelerator team will get some sort of solution, a .close() method would be great…

    I would add that I think the memory leak happens on eventHandler because in some cases I call the method 'createHTTPClient' from the root of the js file, and no memory leak happens… Could you test that scenario ?

    Anyway I may have some kind of workaround to make the memory leak smaller :
    xhr2.onload = function(){
    try{
    if(this.status==200) {
    xhr2.abort();
    Ti.API.info("test");

    This way i have less memory leak (By memory leak I mean that in the onload method, the "test" only display once instead of many times, but after several tests it begin again to loop). Could you try this scenario as well and tell me how it goes ?

    Good luck,
    Clem

    — answered November 25th 2010 by clem chaz
    permalink
    0 Comments
  • Hello,

    I'm getting the same issue, which seems pretty big to me, I hope Appcelerator team will get some sort of solution, a .close() method would be great…

    I would add that I think the memory leak happens on eventHandler because in some cases I call the method 'createHTTPClient' from the root of the js file, and no memory leak happens… Could you test that scenario ?

    Anyway I may have some kind of workaround to make the memory leak smaller :

    xhr2.onload = function(){
       try{
          if(this.status==200) {    
             xhr2.abort();
             Ti.API.info("test");
             ...
    

    This way i have less memory leak (By memory leak I mean that in the onload method, the "test" only display once instead of many times, but after several tests it begin again to loop). Could you try this scenario as well and tell me how it goes ?

    Good luck,
    Clem

    — answered November 25th 2010 by clem chaz
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.