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

Android 2.3.3
Eclipse Version: 3.7.0
LogCat

 LogCat  报错信息:

03-15 11:34:43.163: ERROR/AndroidRuntime(17173): java.lang.OutOfMemoryError 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at android.database.CursorWindow.getString_native(Native Method) 、03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at android.database.CursorWindow.getString(CursorWindow.java:329) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at com.tmall.nokia.db.RecordDBHelper.query(RecordDBHelper.java:140) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at com.tmall.nokia.manage.RecordDBopt.get(RecordDBopt.java:80) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at com.tmall.nokia.manage.RecordMessage.sendAll(RecordMessage.java:55) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at com.tmall.nokia.Check.run(Check.java:456) 
03-15 11:34:43.163: ERROR/AndroidRuntime(17173):     at java.lang.Thread.run(Thread.java:1019)

同时有报错信息

03-15 11:30:21.763: ERROR/CursorWindow(17173): need to grow: mSize = 1048576, size = 37, freeSpace() = 5, numRows = 6974
03-15 11:30:21.773: ERROR/CursorWindow(17173): not growing since there are already 6974 row(s), max size 1048576 
03-15 11:30:21.804: ERROR/Cursor(17173): Failed allocating 37 bytes for text/blob at 6973,3 

  或

03-15 11:34:40.463: ERROR/CursorWindow(4647): need to grow: mSize = 1048576, size = 72, freeSpace() = 18, numRows = 6974 
03-15 11:34:40.473: ERROR/CursorWindow(4647): not growing since there are already 6974 row(s), max size 1048576 
03-15 11:34:40.543: ERROR/CursorWindow(4647): The row failed, so back out the new row accounting from allocRowSlot 6973 
03-15 11:34:40.543: ERROR/Cursor(4647): Failed allocating fieldDir at startPos 0 row 6973 
 

 

发生错误原因分析:

 

读取数据库的缓存大约为1M,CursorWindow一次性打开数据太多,造成内存溢出。

数据库中Record表的数据较多,大概有两万条,从以上错误信息,可以看出,大约读取到6974条时报错。 

 

 

解决办法:

 

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

 

你可能感兴趣的:(eclipse,android,sqlite,CursorWindow)