安卓:文件读取 - 内存不足问题 [英] Android: File Reading - OutOfMemory Issue

查看:345
本文介绍了安卓:文件读取 - 内存不足问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建涉及从文件中读取数据的应用程序。该文件是比较大的(1.8 MB)和正在从在的onCreate异步线程读取。应用程序启动时的第一次,它加载就好了。但是,如果您单击后退按钮,然后再加载它,它运行的内存和崩溃(抛出内存溢出错误)。

I am creating an app that involves reading in data from a file. The file is relatively large (1.8 MB) and is being read from an async thread in onCreate. The very first time the app is started up, it loads just fine. However, if you click the back button and then load it again, it runs out of memory and crashes (throwing the OutOfMemory error).

我如何得到它使用的内存的最低金额和/或免费的,当它完成记忆?

How do I get it to use the lowest amount of memory possible and/or free that memory when it is done?

文件阅读code(在异步类的 doInBackground()方法执行):

File Reading Code (executed in the doInBackground() method of the async class):

public ArrayList<String> createDataList() {
    dataList = new ArrayList<String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "text.txt")));
        String data;
        while ((data = br.readLine()) != null) {
            dataList.add(data);
        }                           
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    return dataList;
}

编辑*
异步类:

EDIT* Async Class:

private class loadData extends AsyncTask<Void, Void, ArrayList<String>> {

    @Override
    protected ArrayList<String> doInBackground(Void... arg0) {
        dataList = createDataList();
        return dataList;
    }

    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        // display first element in ArrayList in TextView as a test

    }

}

我曾尝试基于我要如何组织数据并从每个文本文件中的数据存储到一个单独的的ArrayList 分裂的文件,但我有记忆问题这点。我还保存所有的数据整合到一个主的ArrayList ,然后调用上的主人的方法,将数据添加到相应的的ArrayList (删除/只要它复制从主清除数据)。

I have tried splitting up the file based on how I want to organize the data and store the data from each text file into a separate ArrayList but I had memory problems with that as well. I have also stored all of the data into one "master" ArrayList and then invoked a method on that "master" to add the data to the appropriate ArrayList (removing/clearing the data from the "master" as soon as it copied).

如何精简,减少内存的影响任何想法?

Any ideas on how to streamline and reduce memory impact?

编辑**

logcat的:

这是当您单击后退按钮,然后再加载活动。下面只是生产的消息之一(在详细):

That is from when you click the back button and then load the activity again. The following is just one of the messages produced (in verbose):

推荐答案

首先,确保你没有数据的两个副本在内存中。你可以开始创建新的前空出的参考旧的ArrayList,但你要仔细做到这一点 - 调用 ArrayList.clear()第一会更彻底。

First, make sure you don't have two copies of the data in memory. You can null out the reference to the old ArrayList before starting to create the new one, though you have to do that carefully -- calling ArrayList.clear() first would be more thorough.

二,计算出有多少内存ArrayList是使用像 HPROF 或Eclipse的MAT工具吃起来。如果它使用的是吨的空间,你可能要考虑一个更紧凑的数据重新presentation。

Second, figure out how much memory that ArrayList is eating up by using a tool like hprof or Eclipse MAT. If it's using a ton of space, you may want to consider a more compact data representation.

所以......从code片段,它看起来像你只是读了一堆文本字符串从一个文件中,用字节到字符转换。如果源材料是普通的UTF-8(即基本上是ASCII),你已经有了一个2倍扩展为UTF-16,再加上分配的char []对象来持有它,加上包装的String对象的大小,加在该ArrayList条目的开销。根据文件中的平均串有多长,这可能是你的一个1.8MB多显著

So... from the code snippet, it looks like you're just reading a bunch of text strings in from a file, using a byte-to-char conversion. If the source material is plain UTF-8 (i.e. essentially ASCII), you've got a 2x expansion to UTF-16, plus allocation of the char[] object to hold it, plus the size of the String object that wraps that, plus the overhead of the entry in the ArrayList. Depending on how long an average string in the file is, this can be a significant multiple on your 1.8MB.

要避免这将是读取文件到内存中字节[] ,扫描,找出每个字符串开始的地方,而只是一味的数组的一种方式与启动整数偏移每个字符串。当你需要串N,德code将其从字节[] 字符串并返回。这显著减少你的开销。您可以根据需要(使用的RandomAccessFile )不加载该文件,只是个人读出字符串进一步降低,但是这可能会慢下来。

One way to avoid this would be to read the file into memory as byte[], scan it to figure out where each string starts, and just keep an array of integers with the start offset of each string. When you need string N, decode it from the byte[] into a String and return it. This reduces your overhead significantly. You could reduce it further by not loading the file and just reading individual strings out as needed (using a RandomAccessFile), but this may slow things down.

这篇关于安卓:文件读取 - 内存不足问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆