Memory usage by widget too high for my liking

JohnK

Active Member
Licensed User
Longtime User
I have a widget that uses more memory than I would like.

In the service, I do cache images in an array for an animation, but then when I am finished with them, I loop through the array and set them all to "null". However, this action does not appear to have reduced the memory footprint. I would of hoped for the memory to drop down from this action. In the code its actually an array of a user type, where ".Image" is a Bitmap.
B4X:
For i = 0 To ImageCache.Length - 1
   ImageCache(i).Image = Null
   ImageCache(i).IsValid = False
Next
The widget is using about 35MB of ram. Although its not explicitly causing any issues, I just do not like this figure. Is there more I can do to clear the memory? or should I simply rely on the Android garbage collector to look after itself?

Does the memory have any effect on battery usage? On my phone, the battery usage does not show anything under 1%, and my widget does not show up in the list, so I guess its not an issue.
 

thedesolatesoul

Expert
Licensed User
Longtime User
The images will only be garbage collected when there is no reference to it.
Even though you are removing one reference (ImageCache) you need to make sure there are no other references to it.
Also, the link vb1992 might be useful to recycle images no longer in use.
As far as battery usage is concerned, if the memory is just holding your data there should be no issue, but if the OS is running low on resources and keeps need to swap out memory then it could be a problem (still not too much I think).
 
Upvote 0

JohnK

Active Member
Licensed User
Longtime User
I remember reading something about this
not sure if it applies here:

http://www.b4x.com/forum/basic4android-updates-questions/10760-out-memory-2.html#post62144

Thanks. My first go at it, I had a force close. But that "worked" like a dream. The widget was still visible, but the service stopped, and memory went to zero! but of course the widget stopped updating...

Anyway, I fixed that bug, and ran the "recycle" explicitly on all the bitmaps in the array, but it makes no difference. It seems that the canvas I draw them on was holding a reference stopping the clean up. However, this is within the widget, and as such, I cannot get to the objects to call the recycle on them. next?
 
Upvote 0

JohnK

Active Member
Licensed User
Longtime User
No exception. and totally understand that it does not need to be cleaned up, but would simply be more comfortable if I could force it. And yes I understand that this causes CPU cycles in itself. I guess i would like to control when those cycles are used.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I'm tackling a similar problem with large Bitmaps and memory issues.
My research has taken me to this page where - if you take the time to read through all the comments - the garbage collection process is expained quite well.

It looks like the best solution is to call the Bitmap recycle() method and Null all references to the Bitmap once it is no longer in use.

That in itself will not force the Bitmap to be immediately and fully garbage collected, but will mark the memory it is using as 'no longer being used'.

When the garbage collector eventually runs that memory will (in theory) be freed, but there is no way to force the garbage collection to run upon request.

Comment #80 explains it quite well.

Martin.
 
Upvote 0
Top