SQLite一次性读取过多记录,造成内存溢出OutOfMemoryError

前端之家收集整理的这篇文章主要介绍了SQLite一次性读取过多记录,造成内存溢出OutOfMemoryError前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Android 2.3.3 
Eclipse Version: 3.7.0
LogCat

LogCat 报错信息:

03-14 13:16:52.033: ERROR/AndroidRuntime(4399): java.lang.OutOfMemoryError 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at android.database.CursorWindow.getString_native(Native Method) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at android.database.CursorWindow.getString(CursorWindow.java:329) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at com.taobao.htc.db.RecordDBHelper.query(RecordDBHelper.java:140) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at com.taobao.htc.manage.RecordDBopt.get(RecordDBopt.java:80) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at com.taobao.htc.manage.RecordMessage.sendAll(RecordMessage.java:55) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at com.taobao.htc.Check.run(Check.java:456) 
03-14 13:16:52.033: ERROR/AndroidRuntime(4399):     at java.lang.Thread.run(Thread.java:1019)

同时有报错信息


03-14 13:17:24.763: ERROR/CursorWindow(4399): need to grow: mSize = 1048576,size = 37,freeSpace() = 5,numRows = 6974 
03-14 13:17:24.773: ERROR/CursorWindow(4399): not growing since there are already 6974 row(s),max size 1048576 
03-14 13:17:24.804: ERROR/Cursor(4399): Failed allocating 37 bytes for text/blob at 6973,3 

03-14 14:00:59.463: ERROR/CursorWindow(4399): need to grow: mSize = 1048576,size = 72,freeSpace() = 18,numRows = 6974 
03-14 14:00:59.473: ERROR/CursorWindow(4399): not growing since there are already 6974 row(s),max size 1048576 
03-14 14:00:59.543: ERROR/CursorWindow(4399): The row Failed,so back out the new row accounting from allocRowSlot 6973 
03-14 14:00:59.543: ERROR/Cursor(4399): Failed allocating fieldDir at startPos 0 row 6973 


发生错误原因分析:

数据库中Record表的数据较多,大概有两万条,从以上错误信息,可以看出,大约读取到6974条时报错。CursorWindow一次性打开数据太多,造成内存溢出。

解决办法:

查询Record表的数据量,如果过大,做分批处理,每次处理5000条左右的数据,避免读取更多数据造成内存溢出。

猜你在找的Sqlite相关文章