【问题标题】:Android OutOfMemoryError with HashMap and ArrayList带有 HashMap 和 ArrayList 的 Android OutOfMemoryError
【发布时间】:2012-06-14 22:28:13
【问题描述】:

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

}

已修复:

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

【问题讨论】:

  • 您没有将该 CSV 文件读入数据库表并改为查询数据库的任何特殊原因?
  • 我还没有尝试将它读入数据库。在开始之前我做了一些研究,发现很多人的帖子都有很多滞后。我终于找到了我的问题!在研究了活动生命周期后,我发现每次我去我的列表活动选择一个文件并缓存它时都会创建一个新实例,我正在创建我的 MainActivity 的一个新实例。我在清单中将 MainActivity 设置为 singleTop 模式,并将一些代码移至 onNewIntent() 方法,一切都很好。我的应用现在运行良好!

标签: java android arraylist hashmap out-of-memory


【解决方案1】:

一些建议:

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

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

  3. 观看 google's 视频,了解如何查找和处理内存泄漏

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

    使用 Pois 的集合不仅在设计方面会更好(更容易阅读、理解、维护……)。它也将占用更少的空间 - 使用原语而不是包装器(例如 int 而不是整数)。

【讨论】:

  • 此解决方案可能有效,但也可能破坏您的活动流程设计。考虑检查您是否真的必须存储 mapView,而是使用它需要的数据。此外,尝试在清单上设置标志,以防止重新创建活动(如配置更改的那些)。
【解决方案2】:

几个想法:

  1. Integer.toString(1) 可以替换为 "1",这将利用字符串池。
  2. 您是否尝试过使用原语,而不是将所有值存储为 Strings?
  3. latlng 听起来像是在尝试存储纬度和经度。也许您想为此使用Double
  4. 您可以尝试分配一个静态大小的数组并存储长度,而不是使用 ArrayList

【讨论】:

    【解决方案3】:

    我对 CSVReader 的实现了解不多,但它永远不会在 readPOIFile 中关闭。如果它挂在东西上,可能会导致您出现内存问题。

    【讨论】:

      【解决方案4】:

      我检查了对我的 Mapview 的静态引用并删除了静态,但它破坏了我的 getter 和 setter。我终于找到了我的问题!在研究了活动生命周期后,我发现每次我去我的列表活动选择一个文件并缓存它时都会创建一个新实例,我正在创建我的 MainActivity 的一个新实例。我在清单中将 MainActivity 设置为 singleTop 模式,并将一些代码移至 onNewIntent() 方法,一切都很好。我的应用现在运行良好!

      【讨论】:

        猜你喜欢
        • 2014-08-04
        • 2014-09-21
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        相关资源
        最近更新 更多