The section "Avoid Enums Where You Only Need Ints" was removed from the official developer documentation. (See Why doesn't Android use more enums? for the old section content)
Why? Was there a change in the Android VM that made the tip obsolete?
The section "Avoid Enums Where You Only Need Ints" was removed from the official developer documentation. (See Why doesn't Android use more enums? for the old section content)
Why? Was there a change in the Android VM that made the tip obsolete?
The 2011 answer from Elliot Hugues said that the original reason to avoid enum was for performance reason... as in "processing performance". As this reason was not backed by fact, it was removed from the official documentation.
It has been added later on because enums add a lot more data in memory than using integer.
IntDef
annotations, which allow to use int constants safely with the Android Studio errors and warning. blog.shamanland.com/2016/02/int-string-enum.html –
Paleontography the original version of that document was just a bunch of prejudices. it's been rewritten to only contain facts backed up by actual benchmarks, and it's updated as the VM is updated. you can find the various benchmarks -- plus some of the benchmarks we use to optimize the core libraries -- at http://code.google.com/p/dalvik/.
A guess:
Thus, for the comparatively mundane requirements of a GUI app, the development-time benefits of enums far outweigh the extra runtime cost.
Elliott Hughes offers more details about the documentation rewrite on his blog: http://elliotth.blogspot.com/2010/09/java-benchmarks.html
The second half of the post explains that every claim on the Performance doc is now backed up with benchmarks. Previous versions of the doc apparently contained unverified claims, like, "Avoid enums because they are too expensive."
TLDR: Dalvik was not good with memory allocation and Enum
uses more memory than int
. Android Lollipop replaced Dalvik with ART which does not suffer from the same limitations. Thus this recommendation is not relevant anymore.
The long answer:
Wow! 8 years, 5 answers and many comments later the real reason is still not addressed.
In the pre-lollipop Android days, Dalvik was the process VM used. As small amount of memory was available for applications to use during that time, Dalvik had a lot of memory-constraints. For memory allocation Dalvik had to walk the heap and find space. Heap would also get fragmented over time. Dalvik could not defragment, so it would allocate over time and eventually run out of space.
Avoid Enums Where You Only Need Ints
comes from Dalvik days because an Enum
is a lot bigger than an int
and memory allocation was very expensive.
Fast-forward today, Dalvik has been replaced by ART. ART came out in KitKat and is default since Lollipop.
ART was created from the ground up not to optimize for memory but to optimize for performance. It is also optimized for allocations and collections. The reason is it has memory set aside for large objects. Instead of putting everything in the same heap, and then having to find space for large objects amidst all the tiny ones, ART puts all the large objects and bitmaps in a separate heap. And then the small objects go in the separate heap. Also it can defragment.
After ART, if you use Enum
Android does not care and this is why the recommendation is gone now.
This is coming from Chet Haase at Google. I recommend finding his Google I/O talk and watch the whole video. It contains a lot of useful information and insight into Android.
The 2011 answer from Elliot Hugues said that the original reason to avoid enum was for performance reason... as in "processing performance". As this reason was not backed by fact, it was removed from the official documentation.
It has been added later on because enums add a lot more data in memory than using integer.
IntDef
annotations, which allow to use int constants safely with the Android Studio errors and warning. blog.shamanland.com/2016/02/int-string-enum.html –
Paleontography It is still bad for memory performance.
https://developer.android.com/training/articles/memory.html#Overhead
EDIT: Now it is removed. Safe to use enums.
© 2022 - 2024 — McMap. All rights reserved.