Docstoc

Ways To Avoid Memory Leaks In Android

Document Sample
Ways To Avoid Memory Leaks In Android Powered By Docstoc
					Android applications especially the android mobile applications are
limited to 16GB heap. For a phone it is a lot of memory. But then for the
offshore development team it is very little for them to achieve what they
want. One might not use the entire memory space but then at least a
little memory space should be used by the Android mobile applications. If
the Android is able to take more applications in the memory the user will
have the flexibility of switching between different Android development
applications. But then there are memory leaks in the android
applications. This I an issue and most of the time this happens because
of the same or similar mistake.

 In android the term context is used for a lot of operations. But then it
is mainly used to access and to load resources. This is why there is a
context parameter in the constrictor of a widget. There are normally two
types of context, they are application and activity. It is the activity
that is normally passed to the classes by the offshore Android
development team.

 @Override
 protected void onCreate(Bundle state) {
 super.onCreate(state);

 TextView label = new TextView(this);
 label.setText("Leaks are bad");

 setContentView(label);
 }

 The above means when the view is able to have a complete reference to
the whole activity and also to anything that the activity is holding on.
Thus when there is a small leak also it will land up in leaking out a lot
of memory. An entire activity can be leaked easily if proper care is not
taken.

 When there is a change in the orientation of the screen the default
setting of the system is that the current activity will be completely
wiped out and totally new one is made. The android mobile application
will actually reload the user interface of the application from resources
while doing this. Now let us take for instance Android development team
has written an application for which the bit map is huge and you also do
not want it to get reloaded during every rotation. In this case the best
way to see to that the reload does not happen is to have a static field.

 private static Draw able sBackground;

 @Override
 protected void onCreate(Bundle state) {
 super.onCreate(state);

 TextView label = new TextView(this);
 label.setText("Leaks are bad");

 if (sBackground == null) {
 sBackground = getDrawable(R.drawable.large_bitmap);
 }
 label.setBackgroundDrawable(sBackground);

 setContentView(label);
 }

 "The code that is mentioned above is very quick but then it is not
completely right as the first activity is leaked in this. a view is
placed as a callback on a draw able when the draw able is attached to the
view. This means the draw able will have a reference to Text view.
 "This is the basic memory leak in case of Context. Of course one can
create a chain ok the leak context but then it is not good as it will
occupy a lot of memory space.
 "To shun contest memory leaks there are two ways. The one that is
commonly known to all is to evade escaping the context outside. The above
example is a static reference case. The other option is to use the
Application context in Android mobile applications.

				
DOCUMENT INFO
Categories:
Stats:
views:4
posted:5/23/2012
language:
pages:2
Description: Have an Android phone but don't know what apps are worth downloading or purchasing? Android phones are excellent for those who love apps on a daily basis and want an easy way to play games, store information, and do other simple tasks with every unique app. Here are the top ten apps for any Android phone user ranging from games, entertainment to tools and devices: