I'm developing a dictionary using *.txt files in /raw directory, also I have a history (current 18 entries).
Every OnResume()
I'm getting history entries from file on SDCard and filling ListArray
's than use ArrayAdapter
to fill a ListView
.
I can't understand why I have a big memory leak (every onResume()
adds about 4-6 MB to the memory). Please help me.
Here is my code:
public class SecondTab extends Activity {
ListView lv1;
ArrayList <String> ArrayHist = new ArrayList <String>();
ArrayList <String> ArrayHistMin = new ArrayList <String>();
BufferedReader Buffer;
InputStream file;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hist);
SetContent();
if (ru.andr.dictu.FirstTab.myErrorInHist)
{
Toast.makeText(this, getString(R.string.err_hist), Toast.LENGTH_LONG).show();
}
}
public void SetContent()
{
//show History entries
//trying to solve memory leak
try
{
ArrayHist.clear();
ArrayHistMin.clear();
}
catch (Exception e){}
ArrayHist=null;
ArrayHistMin=null;
ArrayHist = new ArrayList <String>();
ArrayHistMin = new ArrayList <String>();
Buffer=null;
file=null;
if (ru.andr.dictu.FirstTab.myErrorInHist!=true)
{
//filling arrays
try {
file = new FileInputStream(ru.andr.dictu.history_func.File_hist()); //getting name of file from common store
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Buffer = new BufferedReader(new InputStreamReader(file));
try {
String Str;
int counter_hist_content = 0;
while ( (Str = Buffer.readLine()) != null){ //reading from history file
String myTrimStr = Str.trim();
ArrayHistMin.add(myTrimStr.substring(0, myTrimStr.indexOf(";;")).intern()); //main word
ArrayHist.add(myTrimStr.substring(myTrimStr.indexOf(";;")+2).intern()); //ususaly translate
if (counter_hist_content==50) break;//needs only 50 entries
counter_hist_content++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
//closing files, buffers
file.reset();
file.close();
Buffer.reset();
Buffer.close();
}catch (Exception e) {}
}
lv1 = (ListView)findViewById(R.id.history);
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_hist, ArrayHistMin));
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
changeClass (position , ArrayHist.get(position));
}
});
lv1.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ru.andr.dictu.myspeak.text=null;
ru.andr.dictu.myspeak.text=ArrayHistMin.get(arg2);
if (ru.andr.dictu.myspeak.text.indexOf("[")!=-1)
ru.andr.dictu.myspeak.text=ru.andr.dictu.myspeak.text.substring(0,ru.andr.dictu.myspeak.text.indexOf("[")).intern();
speakClass();
return true;
}
});
}
public void speakClass() {
Intent intent = new Intent();
intent.setClass(this, myspeak.class);
startActivity(intent);
}
public void changeClass(int position, String extArray) {
Intent intent = new Intent();
intent.setClass(this, List.class);
intent.putExtra(List.results, extArray.toString().intern());
startActivity(intent);
getParent().overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
@Override protected void onPause() {super.onPause(); }
@Override
protected void onResume()
{
super.onResume();
SetContent();
}
My guess is the new InputStreamReader(file) is being leaked. You need to close this reader.
ReplyDeleteThough if this does not solve the problem, dump the hprof data and check using MAT tool in eclipse. You can point out which class is taking maximum heap.
Edit: You can dump hprof in DDMS view. It is one of the buttons right above where processes are displayed