且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:文件读取 - 内存不足问题

更新时间:2023-08-19 16:35:04

首先,确保你没有数据的两个副本在内存中。你可以开始创建新的前空出的参考旧的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.