Enums in Android

Ounces equal pounds, pounds equal pain

I’ve recently been interviewing around for Android Developer positions, and something that surprised me was how frequently my interviewer would ask language specific questions. I had ( wrongly ) assumed that most questions would focus on the Android platform, as that’s what most of the Glassdoor interview questions seemed to focus on. Since then I’ve started devoting a little more time to researching and learning about ways to use Java’s unique properties to make my applications more efficient and bug free.

I think that for a number of developers working on something as specific as Android, there can be an almost blind focus on learning the newest and hottest libraries, instead of really nailing down their fundamentals. As such, I’m going to cover a little topic I’ve been researching and playing around with, and hopefully you’ll take it into consideration next time you open up Android Studios.

I was about to use an Enum class for an application I’ve been working on, and in my research I came across this fragment on Stack Overflow. So much love for that site. Coming straight from from the Google Developers training site is this quote about using Enums in apps:

Be aware of memory overhead

Be knowledgeable about the cost and overhead of the language and libraries you are using, and keep this information in mind when you design your app, from start to finish. Often, things on the surface that look innocuous may in fact have a large amount of overhead. Examples include:

  • Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
  • Every class in Java (including anonymous inner classes) uses about 500 bytes of code.
  • Every class instance has 12-16 bytes of RAM overhead.
  • Putting a single entry into a HashMap requires the allocation of an additional entry object that takes 32 bytes (see the previous section about optimized data containers).

A few bytes here and there quickly add up—app designs that are class- or object-heavy will suffer from this overhead. That can leave you in the difficult position of looking at a heap analysis and realizing your problem is a lot of small objects using up your RAM.

Full page here: https://developer.android.com/topic/performance/memory.html#Overhead

Little things like this are what I live for. Maintaining the mindset that ounces equal pounds and cents equal dollars has made me a more attentive and efficient person, and I think we could all use more of this mindset when developing for fun and on the job.

Thanks for reading, and happy coding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s