View.GONE - a surprising pitfall
Updated: Jan 19, 2020
Memory usage should concern any android developer.
If you optimize your memory consumption you will gain smoother user experience and smoother animations due to less GC (garbage collections), Moreover,
it will assist you to avoid the infamous OOM crash (Out Of Memory) - especially when low-level devices are all over the place.
In this post, I will not talk about memory leaks and fancy tools to track them, I want to address a simple and common misconception among developers, it is related to view visibility (View.Gone).
They think that if they are setting the visibility of a view to GONE instead of INVISIBLE then the OS will not load it to memory until they will set it to VISIBLE - well... that's wrong.
The problem is even worse when these views have a large background image that weighs a lot, the result is that the OS will load this image to the memory without any need. Action Item? well, It is OK to keep the Views with View.GONE in your layouts but you should load the background/image drawable on runtime just before you actually want to make them visible. I encourage you to go over your source code and track those expensive ghost views down.