使用数组和文件的内存泄漏android

时间:2012-01-15 11:13:06

标签: android memory-leaks

我正在使用/ raw目录中的* .txt文件开发一个字典,我也有一个历史记录(当前18个条目)。

OnResume()我从SDCard上的文件中获取历史记录条目并填写ListArray,而不是使用ArrayAdapter来填充ListView

我无法理解为什么我有大量的内存泄漏(每onResume()增加大约4-6 MB的内存)。请帮帮我。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

我的猜测是new InputStreamReader(file)被泄露了。你需要关闭这个读者 虽然如果这不能解决问题,请转储hprof数据并在eclipse中使用MAT工具进行检查。您可以指出哪个类占用最大堆。

编辑:您可以在DDMS视图中转储hprof。它是显示流程的正上方按钮之一