我有一個內存問題,我找不出來。我有一個課程可以完成我所有的數據庫檢索工作。我的錯誤是:當我這樣做時分配光標時內存不足
android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=733 (# cursors opened by this proc=733)
內存分配錯誤的:
mDatabaseInterface.getGraphForLevel(level);
我知道這是一個泄漏,因爲我粗略地調用這個方法每2.5秒,並且5或者6個第一個呼叫容易通過。現在,這裏是我DatabaseInterface類中的方法:
public Graph getGraphForLevel(Level level) {
//get the nodes
ArrayList<Node> nodes = new ArrayList<Node>(Arrays.asList(this.getNodesWithLevel(level)));
//get the edges
ArrayList<Edge> edges = new ArrayList<Edge>(Arrays.asList(this.getEdgesWithNodes(nodes)));
return new Graph(nodes, edges);
}
public Node[] getNodesWithLevel(Level level) {
List<Node> l = new ArrayList<Node>();
Cursor cursor = mDatabase.query("nodes", null,
"level = " + wrapSql(String.valueOf(level.getId())), null, null, null, null);
while (cursor.moveToNext()) {
l.add(parseNodeFromCursor(cursor));
}
cursor.close();
return l.toArray(new Node[l.size()]);
}
private Node parseNodeFromCursor(Cursor cursor) {
Level l = getLevelWithId(cursor.getInt(2));
return new Node(cursor.getInt(0), cursor.getString(1), l,
cursor.getInt(4), cursor.getInt(5));
}
我有很多的相互調用的方法,但我知道,因爲這個類在另一個應用程序的工作原理它不是一個遞歸問題。我的主要問題是爲什麼不解開光標cursor.close()
?如果我這樣做:
cursor = mDatabase.query(...);
cursor.moveToNext();
Node node = new Node(cursor.getInt());
cursor.close();
遊標是否保留在那種情況下?
在此先感謝。
只爲了解一個想法,節點和邊的表有多大? – Matthieu
他們現在非常小,20行和10列最多 – chopchop