使用Android加载1,440,000大小的ArrayList时操作极其缓慢

时间:2012-04-26 15:20:25

标签: android arraylist

源代码如下所示

public static ArrayList<Integer> readFile(String fileName) {

String sdPath;
sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
ArrayList<Integer> data = new ArrayList<Integer>();

try {

Scanner s = new Scanner(new FileInputStream(new File(sdPath + fileName)));

while( s.hasNext() ) {
data.add( s.nextInt() );
}
}
catch(FileNotFoundException fN) {
fN.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
}

return data;    
}

当我运行此代码时,LogCat会在我的android

中显示以下消息并显示任何更改
.
.
.
04-27 00:12:31.216: D/dalvikvm(17141): GC_CONCURRENT freed 1953K, 23% free 15699K/20295K, paused 2ms+12ms
.
.
.

此外,上面的代码可以在几秒钟内在我的桌面上完全运行..

请帮助我。

2 个答案:

答案 0 :(得分:3)

与台式电脑相比,手机的资源非常有限。您不应期望手机能够以与台式计算机相同的速度完成计算密集型任务......

你正在耗尽所有试图将大量ArrayList加载到其中的手机RAM。

一些提示:

  • 如果不存在,则将readFile代码移动到后台线程。
  • 考虑一种不同的数据结构,某种SQL可能会更有效地处理这么大的数据集
  • 考虑一次只加载一个(相对)小部分的ArrayList。在你需要的部分工作,当你需要另一个部分分别加载时。
  • 考虑将您的数据分解为多个较小的文件,并仅加载当前操作所需的文件。

答案 1 :(得分:0)

只需将数据保存在磁盘上,然后使用内存映射IO。看看FileChannel and the map function然后从那里拿走它。缓冲类也有methods to retrieve integers