Skip to main content

Memory-leak android using arrays and files



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();

}




Comments

  1. My guess is the new InputStreamReader(file) is being leaked. You need to close this reader.
    Though 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.