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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex