I'm having trouble finding good information about garbage collection in Android.
I don't really understand when you have a memory exception
Here's a question:
When I run my application and monitor it with the Android Device Monitor I see that the allocated memory is 8 MB when the application has just started.
There's a button that creates an object each time it's pushed.
So when i keep pushing the button, the allocated memory grows until it reaches the heap size. When the heap size is reached, the allocated memory goes back to 8 MB but the heap has grown a little bit. After that, this whole thing happens again and the heap keeps growing
Do i have a memory exception in this case? or is it normal that the heap size keeps growing?
The heap size is sort of reserved memory you can use. The allocated memory is the actual memory in use.
When the allocated memory is nearing the heap size, and you see the allocated memory drop, objects are being garbage collected. The heap size will grow a bit, since you probably need more memory than currently reserved.
In the end, if you keep allocating memory without releasing it, the heap size and allocated memory will grow, until the system runs out of memory (for your application). At that point, an OutOfMemoryException
will be thrown, and your app will crash.
Read Managing Your App's Memory | Android Developers for a deeper insight.