Free images from cache

Is there any way to clear images that are cached in memory? We have a feature in our app that allows users to upload images from their photo albums to our servers. If they decide to change their image of choice that they want to upload I can not have the previous image that was loaded into the app replace with the new one that they load up. The old one remains cached the entire session. And also all the images they upload are all cached the entire time so if they upload too many the app crashes…

Is there any way to manually free images from memory? We have an urgent update we’re trying to push out asap but this bug is holding us back… Appreciate all input from anyone.

Thanks! [import]uid: 14018 topic_id: 32460 reply_id: 332460[/import]

Corona/Lua frees images automatically, there is no way to directly free them manually.

To get the memory management system to remove cached images, you need to remove all references to the images. You do this by removing the object from any display hierarchy (object:removeSelf() ), and also by nilling out ALL references to the object (object = nil).

If you re-use the same variable for another graphic, it’s recommended that you remove the old graphic from and display hierarchy and nil it before assigning the new image to the variable (as above).

If the lua memory manager detects that ANY variable could possibly still be pointing at an image, it will not free the memory. All references to the image must be removed (object:removeSelf(), object = nil)

Another caching issue, which it doesn’t sound like is affecting you, is that corona caches image filenames as well. But it doesn’t sound like it’s that caching that’s giving you the trouble. [import]uid: 79933 topic_id: 32460 reply_id: 129095[/import]

mpappas is right. You have to let go of all references to your image in Lua. That said, the Lua garbage collector will likely not destroy the image immediately. In order to force Lua to collect garbage, you must call the following function…
[lua]collectgarbage()[/lua]

I only recommend that you force garbage collection if you know you need to load a lot of images immediately after releasing your previous images, such as during a storyboard or director transition. Depending on your app, this may be necessary step on low-end memory starved Android devices. [import]uid: 32256 topic_id: 32460 reply_id: 129110[/import]

Corona/Lua frees images automatically, there is no way to directly free them manually.

To get the memory management system to remove cached images, you need to remove all references to the images. You do this by removing the object from any display hierarchy (object:removeSelf() ), and also by nilling out ALL references to the object (object = nil).

If you re-use the same variable for another graphic, it’s recommended that you remove the old graphic from and display hierarchy and nil it before assigning the new image to the variable (as above).

If the lua memory manager detects that ANY variable could possibly still be pointing at an image, it will not free the memory. All references to the image must be removed (object:removeSelf(), object = nil)

Another caching issue, which it doesn’t sound like is affecting you, is that corona caches image filenames as well. But it doesn’t sound like it’s that caching that’s giving you the trouble. [import]uid: 79933 topic_id: 32460 reply_id: 129095[/import]

mpappas is right. You have to let go of all references to your image in Lua. That said, the Lua garbage collector will likely not destroy the image immediately. In order to force Lua to collect garbage, you must call the following function…
[lua]collectgarbage()[/lua]

I only recommend that you force garbage collection if you know you need to load a lot of images immediately after releasing your previous images, such as during a storyboard or director transition. Depending on your app, this may be necessary step on low-end memory starved Android devices. [import]uid: 32256 topic_id: 32460 reply_id: 129110[/import]

Thanks for the answers, @mpappas, actually that was the root of the problem. The file uploaded by users was saved to the TemporaryDirectory as “upload-1.jpg”, if they decided to change the image uploaded, upload-1.jpg would remain as the previous image. Looking into the sandboxed folder in the simulator I could see that the image in there was replaced with the new one, but loading the new one up into the app would give me the old image again.

We solved it by appending +1 to the filename for each time they re-upload an image, so uploading 2 images would name the second image upload-11.jpg. But still it would be great to somehow clear the filename from cache? Does the caching of the filenames consume memory?

Appreciate the help! [import]uid: 14018 topic_id: 32460 reply_id: 129219[/import]

There’s no way I know of to clear coronas filename list it uses for caching. As far as the memory it uses (for just the filenames) I would guess it’s pretty minimal, and you’d have to have many many thousands (if not millions) before it would affect anything.

As far as unique filenames go, you could try something like:

 fileName = tostring(mojoData.userID)   
 fileName = fileName .. tostring(os.time())  

This would give every file your users uploaded a unique filename. Users files could even be stored in the same folder, without naming collisions. The limitation is that users could only create one file per second (os.time resolution).

Glad I could help, best of luck with your app. [import]uid: 79933 topic_id: 32460 reply_id: 129232[/import]

Mitaten,

See my comment #2 up above. If you remove the temporary image object in Lua via removeSelf(), set the object to nil, and then call collectgarbage() then that would force Corona to release the cached image immediately. Of course, there is nothing wrong with saving new image files to the temp directory either, but just make sure you delete the old files that you are no longer using when your app exits or else your app risks wasting precious storage space on the device.
[import]uid: 32256 topic_id: 32460 reply_id: 129253[/import]

Thanks for the answers, @mpappas, actually that was the root of the problem. The file uploaded by users was saved to the TemporaryDirectory as “upload-1.jpg”, if they decided to change the image uploaded, upload-1.jpg would remain as the previous image. Looking into the sandboxed folder in the simulator I could see that the image in there was replaced with the new one, but loading the new one up into the app would give me the old image again.

We solved it by appending +1 to the filename for each time they re-upload an image, so uploading 2 images would name the second image upload-11.jpg. But still it would be great to somehow clear the filename from cache? Does the caching of the filenames consume memory?

Appreciate the help! [import]uid: 14018 topic_id: 32460 reply_id: 129219[/import]

There’s no way I know of to clear coronas filename list it uses for caching. As far as the memory it uses (for just the filenames) I would guess it’s pretty minimal, and you’d have to have many many thousands (if not millions) before it would affect anything.

As far as unique filenames go, you could try something like:

 fileName = tostring(mojoData.userID)   
 fileName = fileName .. tostring(os.time())  

This would give every file your users uploaded a unique filename. Users files could even be stored in the same folder, without naming collisions. The limitation is that users could only create one file per second (os.time resolution).

Glad I could help, best of luck with your app. [import]uid: 79933 topic_id: 32460 reply_id: 129232[/import]

Mitaten,

See my comment #2 up above. If you remove the temporary image object in Lua via removeSelf(), set the object to nil, and then call collectgarbage() then that would force Corona to release the cached image immediately. Of course, there is nothing wrong with saving new image files to the temp directory either, but just make sure you delete the old files that you are no longer using when your app exits or else your app risks wasting precious storage space on the device.
[import]uid: 32256 topic_id: 32460 reply_id: 129253[/import]

@joshua quick, I must’ve misunderstood the filename caching thing. I assumed when loading up an image to the app, the filename was cached during the entire app-session and even if you completely remove&nil everything pointing to the image and deleting the image, it would still remain cached through the filename.

I’m currently completely nilling out all the objects, removeSelf on all the displayObjects, deleting the imagefile and manually collecting garbage after all this, then I display.save a new image to the same filename and load it up into the app but I still get the old image. I guess I must have forgot a reference to the image somewhere in the code. But I will bring out the magnifier and track it down.

Thanks guys for all the help!

Edit: Found it and nil’d it, now it works. Thanks! :slight_smile: [import]uid: 14018 topic_id: 32460 reply_id: 129454[/import]

@joshua quick, I must’ve misunderstood the filename caching thing. I assumed when loading up an image to the app, the filename was cached during the entire app-session and even if you completely remove&nil everything pointing to the image and deleting the image, it would still remain cached through the filename.

I’m currently completely nilling out all the objects, removeSelf on all the displayObjects, deleting the imagefile and manually collecting garbage after all this, then I display.save a new image to the same filename and load it up into the app but I still get the old image. I guess I must have forgot a reference to the image somewhere in the code. But I will bring out the magnifier and track it down.

Thanks guys for all the help!

Edit: Found it and nil’d it, now it works. Thanks! :slight_smile: [import]uid: 14018 topic_id: 32460 reply_id: 129454[/import]

Glad you got it working! [import]uid: 32256 topic_id: 32460 reply_id: 129553[/import]

Glad you got it working! [import]uid: 32256 topic_id: 32460 reply_id: 129553[/import]