As far as I know, you have no control over the garbage collection. The JavaScript interpreter does this for you.
I did something similar in a project (downloading lots of images) and also ran into memory issues. The most optimal solution I have found is to make sure that garbage collection is possible. The reason for this is that the JavaScript interpreter executes your code and if you loop through all the images and download them, it might not get to do garbage collection before you're done downloading. This means that all the temporary images will still be in memory.
To deal with this issue, you either need to use the defer
-method of underscore.js or the setTimeout
-function. In sense what they both do is that they tell the interpreter that it's okay to run other stuff before this e.g. garbage collection.
This code sample (using underscore.js) should give you the general idea:
var images = [...many entries here...];
function downloadImage(index) {
//Make sure the index is valid
if(index < images.length) {
var httpClient = Ti.Network.createHttpClient({
onload: function() {
//Save the image to the filesystem.
_defer.(downloadImage, ++index); //Download the next image.
}
});
//Run the HttpClient here.
}
}
_.defer(downloadImage, 0); //Start the download sequence.
There are also some tags that you can set in the tiapp.xml file, but in my experience they don't help much.
These three articles on "Keeping your app response" explain the point I'm trying to make quite well:
- Part 1: The problem
- Part 2: The reason
- Part 3: The solution