This question is an extension to this question , that I posted yesterday. I found a possible Memory Leak, and dealt with this the proper way. One of the memory leaks where caused by a non static inner class, more precise a non-static Handler
.
But I am facing a new memory leak issue today. I've read this post, the essence is based around the Context
, Activity or Application Context.
This is the Pie chart that I got from the Eclipse Memory Analyzer
I have seen that there is changes in the Remainder
of this Pie chart, after fixing issues with the Handler
, the Remainder has increased with 10.6 MB.
But over to the Context Issue, the Problem Suspect 2
gives me a hint where to look
35 instances of "android.widget.FrameLayout", loaded by "<system class loader>" occupy 39 218 464 (39,94%) bytes.
Biggest instances:
•android.widget.FrameLayout @ 0x425aa258 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x425d9b20 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x42609258 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x4260a248 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x42925960 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x429808e0 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x429a4350 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x429d9b20 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x429e5710 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x42a28c98 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x42a51b80 - 3 266 728 (3,33%) bytes.
•android.widget.FrameLayout @ 0x46a8caf0 - 3 266 728 (3,33%) bytes.
It tells me that the memory leak occurs where I'm using a FrameLayout
. The only place where I use a FrameLayout, is in my Splash Screen
.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
utils = new AppUtils(getApplicationContext());
pd = (ProgressBar) findViewById(R.id.pd);
progress = (TextView) findViewById(R.id.progress);
version = (TextView)findViewById(R.id.version);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("firstpw", "first");
prefEditor.commit();
folder = new CreateApplicationFolder(getApplicationContext(), true);
boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (!isSDPresent) {
ErrorDialog dialog = new ErrorDialog(getApplicationContext(), getResources().getString(R.string.sdCardTitle),
getResources().getString(R.string.sdCardContent),
getResources().getString(R.string.ok), 2);
Log.i("SDCARD: ", "Is Not Present");
dialog.canCancelDialog(false);
} else {
Log.i("SDCARD: ", "Is Present");
version.setText("Version: " + utils.getAppVersion());
try {
new ReadConfig(Splash.this, pd, progress).execute("");
} catch (ParserConfigurationException e) {
Log.e("Parser: ", e.getMessage());
} catch (SAXException e) {
Log.e("Sax: ", e.getMessage());
} catch (IOException e) {
Log.e("IO: ", e.getMessage());
}
}
}
I suspect that the memory leak occurs when I'm sending the Activity context to the AsyncTask ReadConfig
, like this:
try {
new ReadConfig(Splash.this, pd, progress).execute("");
} catch (ParserConfigurationException e) {
Log.e("Parser: ", e.getMessage());
} catch (SAXException e) {
Log.e("Sax: ", e.getMessage());
} catch (IOException e) {
Log.e("IO: ", e.getMessage());
}
And in the ReadConfig class, I'm also using this context to start a new Activity
in the onPostExecute
method, which will keep this context alive.
protected void onPostExecute(String result) {
if (result.equals("noConfig")) {
Toast.makeText(context, context.getResources().getString(R.string.configNotFound),
Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(context, MainClass.class);
context.startActivity(mainIntent);
}
else if(result.equals("pathnotFound")) {
new ErrorDialog(context, context.getResources().getString(R.string.noBTFolderTitle), context.getResources().getString(R.string.noBtFolderContent), context.getResources().getString(R.string.exit), 2);
} else {
Intent mainIntent = new Intent(context, MainClass.class);
context.startActivity(mainIntent);
}
}
I need the context to start this new Activity, getting strings from the resources and so on. Is there any possible way I can do this differently to avoid the memory leak? Thanks for reading this big post, and just add a comment if you need more code or anything else.
Thanks in advance.