Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中关闭光标如何防止内存泄漏?_Android_Sqlite_Cursor_Android Cursor - Fatal编程技术网

在Android中关闭光标如何防止内存泄漏?

在Android中关闭光标如何防止内存泄漏?,android,sqlite,cursor,android-cursor,Android,Sqlite,Cursor,Android Cursor,我正在学习Android中有关SQLite数据库的游标。我使用以下方法从宠物收容所数据库中检索宠物信息,并将其显示在简单的文本视图中: 私有void displayDatabaseInfo(){ //创建和/或打开数据库以从中读取 SQLiteDatabase db=mDbHelper.getReadableDatabase() 我只是不明白为什么我们必须关闭游标。课程中说这是为了避免内存泄漏,但游标对象是方法中的局部变量,在方法完成后可以进行垃圾回收。那么为什么我们仍然需要调用close()

我正在学习Android中有关SQLite数据库的游标。我使用以下方法从宠物收容所数据库中检索宠物信息,并将其显示在简单的文本视图中:

私有void displayDatabaseInfo(){ //创建和/或打开数据库以从中读取 SQLiteDatabase db=mDbHelper.getReadableDatabase()

我只是不明白为什么我们必须关闭游标。课程中说这是为了避免内存泄漏,但游标对象是方法中的局部变量,在方法完成后可以进行垃圾回收。那么为什么我们仍然需要调用close()

我一直在做一些研究,并在这里查看其他问题和答案。这一个给出了一个提示:。它说

或者自己关闭它。不这样做会泄漏内存,GC不会 帮助解决这个问题,因为光标后面有本机资源(例如。 数据库的文件句柄)

所以我可以看出垃圾回收器还不够。但我还不太了解。有人能解释一下这些本机资源吗?为什么垃圾回收不够?提前谢谢。

似乎已经足够好地覆盖了资源泄漏。行为不一致,并不总是很明显,但已经讨论过了但也有一些副作用

    // Perform query
    Cursor cursor = db.query(PetEntry.TABLE_NAME,
            null,
            null,
            null,
            null,
            null,
            null);

    TextView displayView = (TextView) findViewById(R.id.text_view_pet);

    try {       
        displayView.setText("The pets table contains " + cursor.getCount() + " pets.\n\n");
        displayView.append(PetEntry._ID + " - " +
                PetEntry.COLUMN_PET_NAME + " - " +
                PetEntry.COLUMN_PET_BREED + " - " +
                PetEntry.COLUMN_PET_GENDER + " - " +
                PetEntry.COLUMN_PET_WEIGHT + "\n");

        // Figure out the index of each column
        int idColumnIndex = cursor.getColumnIndex(PetEntry._ID);
        int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
        int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
        int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);
        int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);

        // Iterate through all the returned rows in the cursor
        while (cursor.moveToNext()) {
            // Use that index to extract the String or Int value of the word
            // at the current row the cursor is on.
            int currentID = cursor.getInt(idColumnIndex);
            String currentName = cursor.getString(nameColumnIndex);
            String currentBreed = cursor.getString(breedColumnIndex);
            int currentGender = cursor.getInt(genderColumnIndex);
            int currentWeight = cursor.getInt(weightColumnIndex);
            // Display the values from each column of the current row in the cursor in the TextView
            displayView.append("\n" + currentID + " - " +
                    currentName + " - " +
                    currentBreed + " - " +
                    currentGender + " - " +
                    currentWeight);
        }
    } finally {
        cursor.close();
    }
}