2048 KB的SQLite数据库Android的光标窗口分配失败光标、分配、窗口、数据库

2023-09-12 03:07:35 作者:芳草亦倾心

我有违背每秒SQLite数据库许多次不同的查询的程序。过了一会儿,我会得到错误

I have a routine that runs different queries against an SQLite database many times per second. After a while I would get the error

android.database.CursorWindowAllocationException: - 2048 KB光标窗口分配失败#打开游标=。出现在L​​ogCat中

"android.database.CursorWindowAllocationException: - Cursor window allocation of 2048 kb failed. # Open Cursors = " appear in LogCat.

我的应用程序日志内存使用情况,而且确实在使用量达到一定限度的,我得到这个错误,这意味着它用完。我的直觉告诉我,数据库引擎正在创造一个新的缓冲区(CursorWindow)我每次运行一个查询时,即使我标志着.close()光标,无论是垃圾收集器,也没有 SQLiteDatabase.releaseMemory ()是在释放内存速度不够快。我认为,解决方案可能在于逼迫数据库总是写入同一个缓冲区,而不是创造新的,但我一直无法找到一个方法来做到这一点。我曾尝试实例化自己的CursorWindow,并试图将其设置为和SQLiteCursor无济于事。

I had the app log memory usage, and indeed when usage reaches a certain limit the I get this error, implying it runs out. My intuition tells me that the database engine is creating a NEW buffer (CursorWindow) every time I run a query, and even though I mark the .close() the cursors, neither the garbage collector nor SQLiteDatabase.releaseMemory() are quick enough at freeing memory. I think the solution may lie in "forcing" the database to always write into the same buffer, and not create new ones, but I have been unable to find a way to do this. I have tried instantiating my own CursorWindow, and tried setting it to and SQLiteCursor to no avail.

¿任何想法?

编辑:再比如code从@GrahamBorland要求:

re example code request from @GrahamBorland:

public static CursorWindow cursorWindow = new CursorWindow("cursorWindow"); 
public static SQLiteCursor sqlCursor;
public static void getItemsVisibleArea(GeoPoint mapCenter, int latSpan, int lonSpan) {
query = "SELECT * FROM Items"; //would be more complex in real code
sqlCursor = (SQLiteCursor)db.rawQuery(query, null);
sqlCursor.setWindow(cursorWindow);
}

在理想情况下,我想能够 .setWindow()给人一种新的查询,并且已经将数据放入同一个 CursorWindow 每次我获得新的数据。

Ideally I would like to be able to .setWindow() before giving a new query, and have the data put into the same CursorWindow everytime I get new data.

推荐答案

最常见的原因这个错误是不关闭游标。请确保您关闭所有游标使用它们(即使是在一个错误的情况下)之后。

Most often the cause for this error are non closed cursors. Make sure you close all cursors after using them (even in the case of an error).

Cursor cursor = null;
try {
    cursor = db.query(...
    // do some work with the cursor here.
} finally {
    // this gets called even if there is an exception somewhere above
    if(cursor != null)
        cursor.close();
}
 
精彩推荐
图片推荐