Buy @ Amazon

Search This Blog

July 11, 2017

Android Development ProTips To Prevent Memory Leaks

Memory Leaks are not apparent during development
This post is for those impatient Android developers who are seeking a checklist of tips to remember during development, in order to avoid the nasty Memory Leaks in the app thus leading to poor app performance. Poor app performance results in bad user experience that leads to poor ratings in the Play Store.

Let's get the basics right. As much as we'd like to think of Java's Garbage Collector as our reliable agent to cleanse heap of stale objects, it's not hundred percent fool-proof, in that some of the overlooked coding gotchas can trip its reliability. For instance, a WeakReferenced object gets garbage collected quickly over its Strongly Referenced object. But that said, using WeakReferenced object comes in the way of elegant coding. So, typically you look for for areas where you can introduce WeakReference.

ProTip #1: Where possible, use the context-application instead of a context-activity. Look for opportunities where the Application-context is sufficient, instead of the Activity-context.

ProTip #2: Have ImageViews in Activity/Fragment? Use WeakReference to hold the ImageView, so that when the Activity/Fragment is destroyed, the ImageView is readily Garbage Collected.

ProTip #3: Replace non-static anonymous inner-classes (whose life-cycle is not in your control) with static inner-classes. In Java, the non-static inner-classes, hold an implicit reference to its containing class. So imagine you use a non-static inner class inside of your activity. Even after the activity is past the onDestroy life-cycle, it wouldn't be garbage collected, because the non-static inner class holds an implicit reference to it.

ProTip #4: Using Handler? Then don't forget to call removeCallbacksAndMessages(null) or removeCallbacks(mRunnable) in the onDestroy() lifecycle method of the class containing it.

ProTip #5: Using any Listeners? Remember to unregister those listeners.

ProTip #6: Using Eventbus? If you cared to use register(this) then also do care to use unregister(this) in the appropriate lifecycle method definition.

ProTip #7: Employed MVP design-pattern for your code-structure? Passing View implementation as constructor argument to your Presenter implementation? Then ensure to wrap your View in WeakReference.

ProTip #8: Using Threads? Then double-check to see if you have control over its death and are in-fact stopping it at some point in time. Threads in Java are GC roots. Therefore, the Dalvik Virtual Machine (or the DVM) keeps hard references to all active threads in the runtime system, and as a result, threads that are left running will never be eligible for garbage collection.

For detailed and informative reading do care to refer the following posts/articles/books

July 9, 2017

Notes from TED Talk - Why do ambitious women have flat heads?

Watch it in TED

Speaker Profile
In 1962, Dame Stephanie "Steve" Shirley founded Freelance Programmers, a software firm with innovative work practices -- and (mainly) women employees. 
She is a successful million-dollar entrepreneur, Philanthropist and a business-woman in technology. 
She is author of the book (memoir) titled, "Let it Go: The Entrepreneur Turned Ardent Philanthropist"

Secrets to success
  • Surround yourself with first-class people and people that you like. 
  • And choose your partner very very carefully.

Startup Entrepreneurship
It's one thing to have an idea for an enterprise. Making it happen is a very difficult thing. It demands:
  • extra-ordinary energy, self-belief, and determination 
  • the courage to risk family and home 
  • a 24/7 commitment that borders on the obsessive.

Work : "So it's just as well, I'm a workaholic. I believe in the beauty of work when we do it properly and in humility. Work is not just something I do when I'd rather be doing something else."

Life Lesson : We live our life forward. So what has all that taught me? I learned that tomorrow is never going to be like today, and certainly nothing like yesterday. And that made me able to cope with change, indeed eventually to welcome change, though I'm told I'm still very difficult. (Laughter)

July 5, 2017

Colorful Android Logs for Better Productivity

This post assumes you're using Android Studio, if not you better do ;)

It's very common to keep an eye on the logs in Android Studio, every time we deploy the app-under-development into a device/emulator. And it hurts our eyes and our peace for the increased attention we tend to keep on the rolling logs in the Android Studio hunting for the required pieces of information.

Is there a better way to be a little productive so that you can quickly focus on required logs? There is. By simply color styling your logs based on the log's severity level.

This way, you don't miss on that error log or important debug log amongst the continuously rolling logs since your app is deployed.

The good news is, it's a simple configuration change that you'll have to make in your Android Studio Editor like below and you're done.

Android Studio Preferences -> Search for 'logcat' in the search bar

Define your own color coding for different log severity levels or alternatively feel free to use the one that I ended up copying from.

Assert: 9C27B0
Debug: 2196F3
Error: F44336
Info: 4CAF50
Warning: FFC107

Confession: I've shamelessly copied this technique from one of the StackOverflow answers.

Book Recommendation: If you're a beginner Android developer, don't miss out in grabbing a copy of Android Programming: The Big Nerd Ranch Guide. The authors have done justice in teaching stuff with care wearing the hat of a beginner Android developer. It's worth your time. Happy learning!