Memory leaks in
android:
In this blog I want to explore about memory leaks in android
programming. The main reason for memory leak is not happening proper garbage collection.
It usually happens due to smaller mistakes by programmer but new developer also
does not aware about the same.
Here I want to take an Example: Context object is present in
each android application it provide application context over the application by
getApplicationContext() method. If developer declare any
widgets like textView or editText as a static and pass context object then such
kind of view holds memory and it would not be garbage collected.
How to define memory
leaks:
A failure in program to release discarded memory.
Prevention for memory
leak:
There is few coding practices that should be use by
developer
1 1)
Never make/store a Context or View in a static
variable. This is the first sign of a memory leak
private
static TextView textView; //DO NOT DO THIS
private
static Context context; //DO NOT DO
THIS
2 2)
Do not use more than two same type broad cast
receiver like one is already used in third party which linked with your
application and you also implement in your application.
3 3)
Don’t store strong references to activities in
your AsyncTasks or background threads. Your activity may get closed, but your
AsyncTask will continue execution and hold onto that reference of your activity
4 4)
Use Application context (getApplicationContext())
instead of Context from an activity if you can.
5 5)
Proper register and unregister your bound
services in android application.
6 6)
Always try to not use non-static inner classes
if you can avoid it. Storing reference to something like an Activity or View
inside this can lead to memory leaks. Use Weak Reference if you need to store reference to them
Way to fix Memory
Leaks:
It is very tough task to find out the memory leak in android
even then few tools are available using that we can find where the memory utilization
is going across expectation based on this analysis we can find the memory leaks
in android.
1 1)
Memory Monitor
2 2)
Heap Viewer
3 3)
Allocation Tracker
This tools can be used by Android studio .Using these tools
user can analyze their application memory utilization and able to find out in
which activity or part of application taking more memory. Try to optimize the
code on required places and avoid the above written prevention things if
possible.
For more details of these tools you can go through the
developer web site of android.
Hope I have cover most of the memory leak related concept
and their prevention. If something more you know about this and I did not mansion
in this so you can post the same.
görüntülüshow
ReplyDeleteücretli show
MOQM3W
https://titandijital.com.tr/
ReplyDeletebalıkesir parça eşya taşıma
eskişehir parça eşya taşıma
ardahan parça eşya taşıma
muş parça eşya taşıma
XVİ