Tuesday, 31 March 2015

Avoid MEMORY LEAKS in Android

What is a memory leak?

Memory leaks means resources which are not available for GC (Garbage Collection).

There are two basic rules for writing efficient code:

  • Don't do work that you don't need to do.
  • Don't allocate memory if you can avoid it.
 Before starting, an Android developer should keep in mind that:

Android applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It's both a lot of memory for a phone and yet very little for what some developers want to achieve. Even if you do not plan on using all of this memory, you should use as little as possible to let other applications run without getting them killed. The more applications Android can keep in memory, the faster it will be for the user to switch between his apps. As part of my job, I ran into memory leaks issues in Android applications and they are most of the time due to the same mistake: keeping a long-lived reference to a Context.

Know about Context:

On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context,
            1) Activity and
            2) Application.
It's usually the first one that the developer passes to classes and methods.

Now coming to the subject, GC will free resources which are not referenced. Sometimes a single reference can prevent a large set of objects from being garbage collected. eg: Some static variables which are referenced in an activity may not be for GC, because as long as the reference exists, the Activity will be kept in memory, leaking all of its views.

Lets have a look at a Code snippet :

Here, the static drawable sBackground will leak.

Solution:

The solution here is when the activity gets destroyed, release the drawable in destroy and unbind all callbacks 
Here, our onDestroy() will look like this, 

        
So that the drawable sBackground will be available for GC and can prevent memory leak when activity gets destroyed.
Another common cause for memory leak is non-static inner classes in an Activity. This commonly comes when creating fragments in an Activity. Either create static inner classes or create stand alone classes.

Reference:
  http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html
  http://smartandroidians.blogspot.in/ 

Thursday, 26 March 2015

Android Remote Crash

 We can get crash data (stack traces) from Android application when working on our own device being retrieved by cable, But how to get the crash report from any instance of  app running on remote. This Android tutorial helps you to track the uncatched exception from remote device by letting the users to provide the users feedback along with the following details. All the information are generated into a PDF file and attached and send to your email (Developers mail).
  • Report collected date and time
  • Thread name, Exception name
  • App information like which version of your app is the crashed user using
  • The package name
  •  Device locale, Device model, Android version, Board, Host, Id, Product, Type
  • Memory info like Total memory, Available memory
  • And the Stack trace
 The following steps make you to understand what happen while an uncatched exception occurs in app and the steps to be followed:
  • Attach the library project to the app.
  • Initially we need to register the ExceptionReporter by passing the context of the activity before the super.onCreate(savedInstanceState); method.                                                                                                                                               protected void onCreate(Bundle savedInstanceState) {
            ExceptionReporter reporter = ExceptionReporter.register(this);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EditText sd = (EditText) findViewById(R.id.se);
        }
  • Running the app containing uncatched exception. eg : In XML i have a TextView declared and in JAVA i am referring TextView as EditText (EditText sd = (EditText) findViewById(R.id.textview);) this which will throw a Class cast exception.
  • When an uncaught exception is triggered the reporter creates a notification prompting the users that the app has been crashed and to help fixing the error by sending an error report to the developer.
  • The user decides to send the crash report or cancel the report.
  • If he decides to send report by clicking OK button all the above mentioned information are collected and put into a PDF file and saved externally in a directory named CrashReport and send to developers mail   
I libs i have used
  1. droidText.0.2.jar --> for generating PDF file
  2. mail.jar, additionnal.jar, activation.jar --> for attaching PDF and sending mail 
Note : Don't forget to add permission to the manifest of your app
  1.  <uses-permission android:name="android.permission.INTERNET" />
  2.  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The snap shots favors you to get better understanding :

App crashed and Notification appears:




Dialog that prompts users to enter message and click OK to send mail to developer:
All the PDF generation data gathering and mail sending are done in background.
You can use this features in your app. You can find the whole source code here
happy coding :) Please provide your valuable feedback.