java - HashMap 和 ArrayList 的 Android OutOfMemoryError

标签 java android arraylist hashmap out-of-memory

我正在解析一个 csv 文件,更具体地说是一个 POI 文件,使用 opencsv 并将信息读入 ArrayList。我需要将信息缓存在内存中,因此当用户点击按钮时,我会检查每个 POI 并查看它是否在 map View 的当前范围内。一些 POI 文件可以有 10K - 60K 行。在我的应用程序强制关闭之前,我可以读取大约 50K 行,因此我将限制设置为 30K 以便为其他内容留出内存。我的问题是,当我去加载另一个文件时,我清除了 () Arraylists I trimToSize() 并且我尝试将 ArrayLists 声明为新的 ArrayLists 但 GC 从不从内存中释放旧数据。我可以 clear() 它们并将新文件读入其中,但某些东西不允许 GC 释放内存。我没有接受过编程、IT 或 CS 方面的培训。这是我用 Java/Android 开发或编写的第一个应用程序。我已经工作、阅读和学习了大约 6 天,现在试图弄清楚为什么我有这种内存泄漏。任何帮助将不胜感激,任何关于如何优化我的代码的建议也将不胜感激,因为我是一个完整的菜鸟。此外,下面的代码仅显示了有关将文件读入内存的方法。您可以在 google opencsv 上查看有关其工作原理的文档,如果您需要查看任何其他内容,请告诉我,我会发布。

提前致谢!

    public class MainActivity extends MapActivity implements LocationListener {
    private static MapView mapView;
    int counter = 0;
    private ArrayList<String> arrLat = new ArrayList<String>();
    private ArrayList<String> arrLong = new ArrayList<String>();
    private ArrayList<String> arrName = new ArrayList<String>();
    private ArrayList<String> arrInfo = new ArrayList<String>();
    private ArrayList<Boolean> arrCheck = new ArrayList<Boolean>();
    private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // main.xml contains a MapView
    setContentView(R.layout.main); 

    //Gets file name from ListOfFiles Activity Class
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                boolean callreadPOIFile = extras.getBoolean("callreadPOIFile");
                if(callreadPOIFile) {
                    String filePath = extras.getString("filePath");
                    readPOIFileInThread(filePath);

                }else{
                    // Show user alert box                      
                }

            }
}

public void readPOIFileInThread(String filePath) {


    progressDialog = ProgressDialog.show(this, "", "LOADING:\n" + filePath + "\nPLEASE WAIT...");
    final String finalFilePath = filePath;

    new Thread(new Runnable(){
        public void run(){
            try{
                readPOIFile(finalFilePath);
            }catch(Exception e){
                runOnUiThread(new Runnable() {
                    public void run() {
                Toast.makeText(getApplicationContext(), "Exception, readPOIFileInThread", Toast.LENGTH_SHORT).show();
                //progressDialog.dismiss();
                    }
                });
            }

            progressDialog.dismiss();

        }
    }).start();

}       


//Parse and load POI CSV File
public void readPOIFile(String filePath){


    arrLat.clear();
    arrLong.clear();
    arrName.clear();
    arrInfo.clear();
    arrCheck.clear();

    arrLat.trimToSize();
    arrLong.trimToSize();
    arrName.trimToSize();
    arrInfo.trimToSize();
    arrCheck.trimToSize();

            //arrLat = null;
            //arrLong = null;
            //arrName = null;
            //arrInfo = null;
            //arrCheck = null;

            //arrLat = new ArrayList<String>();
            //arrLong = new ArrayList<String>();
            //arrName = new ArrayList<String>();
            //arrInfo = new ArrayList<String>();
            //arrCheck = new ArrayList<Boolean>();

    System.out.println(arrLat.isEmpty());

    String lat = null;
    String lng = null;
    Double dLat;
    Double dLng;
    int lati;
    int lngi;
    String name = null;
    String info = null;

    CSVReader reader = null;
    //System.out.println(filePath);
    try {
        reader = new CSVReader(new FileReader(filePath));
    } catch (FileNotFoundException e) {
        // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

        // set the message to display
        alertbox.setMessage("There was an error reading file: " + filePath
                + "\n Please check the file format and try again.");

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
                //Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_SHORT).show();
            }
        });

        // show it
        alertbox.show();
        e.printStackTrace();
    }
    String [] nextLine = null;
    int count = 0;
    try {
        while ((nextLine = reader.readNext()) != null) {
            // nextLine[] is an array of values from the line
            //System.out.println(nextLine[0]+ "\n" + nextLine[1]+ "\n"  + nextLine[2]+ "\n"  + nextLine[3] + "\n");

            try {
                lng = nextLine[0];

            } catch (Exception e) {
                lng = Integer.toString(1);
            }

            try {
                lat = nextLine[1];

            } catch (Exception e) {
                lat = Integer.toString(1);
            }
            try {
                name = nextLine[2];

            } catch (Exception e) {
                name = "No Name...";
            }
            try {
                info = nextLine[3];
            } catch (Exception e) {

                info = "No Info...";
            }
            //convert lat and long to double
            try{
                dLat = Double.parseDouble(lat);
                dLng = Double.parseDouble(lng);
            }catch(Exception e){

                System.out.println("error converting lat long to Double at row: " + count);
                break;

            }
            //convert lat lng to int
            lati = (int)(dLat * 1E6);
            lngi = (int)(dLng * 1E6);

            //add line to ArrayLists
            try{
            arrLat.add(Integer.toString(lati));
            arrLong.add(Integer.toString(lngi));
            arrName.add(name);
            arrInfo.add(info);
            arrCheck.add(false);
            }catch (Exception e){

                runOnUiThread(new Runnable() {
                    public void run() {
                        //Toast.makeText(getApplicationContext(), "Error reading. Please check the file. ", Toast.LENGTH_SHORT).show();
                        System.out.println("Error reading file.");

                    }
                });
            }
            count++;
            if(count == 10000 || count == 20000){
                final int showcount = count;
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), showcount + " POI's loaded",
                                Toast.LENGTH_LONG).show();              
                    }
                });
            }

            if(count == 30000)
                break;

            System.out.println(count);
        }
        final String toastFilePath = filePath;
        final int toastcount = count;

        runOnUiThread(new Runnable() {
            public void run() {
                if(toastcount > 0){
                    Toast.makeText(getApplicationContext(), "File: " + toastFilePath + " read... \n"
                            + toastcount + " point(s) were loaded...",
                            Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "INVALIDE FILE!\nFile: " + toastFilePath + " read... \n"
                            + toastcount + " points.",
                            Toast.LENGTH_LONG).show();
                }
            }
        });



    } catch (IOException e) {

        e.printStackTrace();
    }

}

已修复:

我终于找到了我的问题!在研究 Activity 生命周期 a 之后,我发现每次我去我的列表 Activity 选择一个文件并缓存它时都会创建一个新实例,我正在创建我的 MainActivity 的一个新实例。我在 list 中将我的 MainActivity 设置为 singleTop 模式,并将一些代码移动到 onNewIntent() 方法中,一切都很好。我的应用现在运行良好!

最佳答案

一些建议:

  1. 没有对 View 对象(或 drawable 或任何引用上下文的对象)的静态引用。这是一个非常糟糕的做法,很容易导致内存泄漏。原因: 即使在离开 Activity 之后,也会有一个对 View 的静态引用,该 View 引用您离开的 Activity ,包括其所有字段(例如,您的大型集合)。阅读 here了解更多信息。

  2. 您真的必须读取整个文件,并将其全部内容存储到内存中吗?当然,这对您来说很容易,而且比其他任何方法都快得多,但是它很容易占用大量内存,尤其是如果您以这种方式使用它。尝试只读取您需要的内容,并且只存储您需要的内容

  3. 观看google's展示如何查找和处理内存泄漏的视频

  4. 您真的必须将数据存储在字符串中吗?仅需要检查的值(坐标,也许?)或 Pois 集合 怎么样,每个值都有自己的字段(id、名称、坐标、...)? Java 中的字符串是一个字符数组,每个字符占用 2 个字节(因为它是 unicode),因此它会占用大量内存空间。例如,60000 行乘以 80 个字符乘以每个字符 2 个字节是 9,600,000 字节,这几乎是 10MB 。您需要更严格地使用内存。请记住,它是一个移动平台,将内存效率作为其首要任务之一(以便更好地切换任务)。

    使用 Pois 集合不仅在设计方面会更好(更易于阅读、理解、维护...)。它还将占用更少的空间——使用原语而不是包装器(例如,int 而不是 Integer)。

关于java - HashMap 和 ArrayList 的 Android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042221/

相关文章:

java - SimpleDateFormat ("EEEE") 返回错误的时区星期几

java - 在 BroadcastReceiver 中计算耳机按钮的点击次数

java - 如何让 Java 每分钟将 ArrayList 的内容写入文件一次?

Android Canvas 绘制矩形

java - 在 Apache Camel 中对 null body 的这种处理可以更优雅吗?

javascript - Android Emulator 浏览器是否适合并可靠地测试 HTML/Javascript 应用程序?

android - 如何读取 Future<List<GeneratorResult>> 类型

java - 如何在单个 arrayList 中显示 JSON 响应?

java - 3 维数组列表参数不适用

java - 如何在数组列表中保存元素的副本?