So, I am done with a project, now the main issue I am facing is the Memory Leakage in the application (“leak” meaning you keep a reference to an activity, thus preventing the GC from collecting it)
Some of the cases I found, where the memory leakage occurs are:
It occurs because of long lived reference to the activity context.
A very good example of it i found here,
private static Drawable 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);
}
here the problem is with private static Drawable sBackground;
The static Drawable is created with the Activity as the context, so in THAT case, there's a static reference to a Drawable that references the Activity, and that's why there's a leak. As long as that reference exists, the Activity will be kept in memory, leaking all of its views.
The second case that draws the attention is when the screen orientation changes. When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the application’s UI from the resources. Now imagine you wrote an application with a large bitmap that you don’t want to load on every rotation.
This will result in a lot of memory leakage as there could be large bitmaps to load.
The third case, I found was the reference to the activity context. It also results in memory leakage.
I wonder if there is any easy way to avoid such memory leakages from happening. or if there could be tool to check and remove those memory leakages from the application.
To find a memory leak, you've got to look at the system's RAM usage. This can be accomplished in Windows by using the Resource Monitor. In Windows 11/10/8.1: Press Windows+R to open the Run dialog; enter "resmon" and click OK.
To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.
Memory leaks are a class of bugs where the application fails to release memory when no longer needed. Over time, memory leaks affect the performance of both the particular application as well as the operating system. A large leak might result in unacceptable response times due to excessive paging.
I know this is an old post, but recently Square released a library called LeakCanary which is by far the most elegant solution for finding memory leaks.
you can use tools like traceview or memory analyzer to check for memory leaks
http://developer.android.com/tools/help/traceview.html
http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html
Here are some articles i found helpful
http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
http://vahidmlj.blogspot.com/2012/10/android-memory-leak-on-screen-rotation.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With