Android Question How to free up memory?

Sam H

Member
Licensed User
Longtime User
At the moment once I am finished with an object, I simply hide it; "objectName.visibility = False". However, this allows too many objects to build up, and never get deleted. This means, after using my app for a while, it crashes with an error saying it's out of memory: "java.lang.OutOfMemoryError: Java heap space."

Instead of changing the visibility property, I'd like to use a ".uninitialize()" method if there was one or something similar. Will ".removeView()" do the trick, or am I really missing something?

Thanks

Sam
 

Sam H

Member
Licensed User
Longtime User
My app works fine for a while, but after interacting with it for a certain period of time (About 2 minutes of constant full-on use), my app freezes. I have checked the unfiltered log and the only thing that stands out is this line:

GC_FOR_ALLOC freed 256K, 9% free 7615K/8328K, paused 28ms, total 28ms

What does it mean?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
This is perfectly normal for all devices and applications. It is Androids Garbage Collector (GC) doing its job of freeing up memory.

Regards,
RandomCoder
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
In the back of my head I've always wondered the same thing. It seems like there should be a Free method or something similar to tell android that this block of memory can safely be "Garbage Collected" – it will never be used again.

How does one tell android that it can free up the space used by an object? Set enable or visible = False? Remove the view from its parent?

Suppose you have the following example (yes, this is a ridiculous example, you would probably implement it a different way, but it illustrates the question)

Your app shows multicolored bubbles that fall to the ground and explode. The bubbles are represented by panels (square bubbles?) that have their color property set to a random color based on the system clock. Once the bubble hits the ground, it explodes, and the panel is never used again. The app is continually creating new panels and no longer uses panels that have "hit the ground" and exploded.

Once the panel explodes you would like to tell android that it is free to garbage collect the memory used by the panel.

What tells android that this panel is no longer being used?

Barry.
 
Last edited:
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Im guessing that you can use removeview when it is available. But more often than not you should try to reuse objects/views as much as possible. In the example you've set, there may only be 20 bubbles on the screen at any time. When the first bubble (panel 1) hits the ground it can start again from the top , still panel 1 but now bubble 21.
I'm sure Erel will give a more definitive answer in the morning.

Regards,
RandomCoder
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Set the object / view (after removing it) variable to null. The GC will take care of the rest as it looks for memory that is no longer referenced. The GC is automatically called when an attempt to allocate memory fails.

RandomCoder said:
But more often than not you should try to reuse objects/views as much as possible.

Don't agree with that. Object reuse can add an extra layer of complexity to your code. The first goal should be the simplest and most maintainable code that performs the task required. If there is a performance bottleneck then is the time to start looking at object reuse IMHO.
 
Upvote 0

Sam H

Member
Licensed User
Longtime User
thanks, assigning null to objects and removing them from the view seems to have helped a bit. Although I can still crash my app by reloading the graph created enough times.

Thanks for all your comments guys.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
If its a graph that's crashing you're app then its likely a bitmap that's consuming the memory as @Erel stated in the first reply. Are you recreating the graph each time its used or have you declared it as global and keep reusing the same view?

Regards,
RandomCoder
 
Upvote 0

Sam H

Member
Licensed User
Longtime User
If its a graph that's crashing you're app then its likely a bitmap that's consuming the memory as @Erel stated in the first reply. Are you recreating the graph each time its used or have you declared it as global and keep reusing the same view?

Regards,
RandomCoder

I have since changed the graph to a global variable, however the app still crashes, when I reload the graph enough times. More than one graph is drawn each time however so that may be why.
Thanks
Sam
 
Upvote 0
Top