Android:java.lang.OutOfMemoryError虽然数据库非常小

时间:2012-09-22 10:51:37

标签: android database sqlite

我正在开发一个应用程序来存储两个人之间的一些赌注。

现在我对此非常陌生,特别是对于SQLite。问题是,尽管我的数据库中只有2个或3个条目,但我总是得到以下ErrorMessages之一。

否。 1

09-22 10:19:36.138: E/AndroidRuntime(21351): FATAL EXCEPTION: main
09-22 10:19:36.138: E/AndroidRuntime(21351): java.lang.OutOfMemoryError
09-22 10:19:36.138: E/AndroidRuntime(21351):    at java.util.Vector.newElementArray(Vector.java:120)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at java.util.Vector.growByOne(Vector.java:466)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at java.util.Vector.add(Vector.java:153)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at challenge.sql.CustomDataSource.getAllEntries(CustomDataSource.java:172)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at challenge.main.OverviewActivity.updateOverview(OverviewActivity.java:40)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at challenge.main.OverviewActivity.onCreate(OverviewActivity.java:29)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.Activity.performCreate(Activity.java:4465)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.os.Looper.loop(Looper.java:137)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at android.app.ActivityThread.main(ActivityThread.java:4340)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-22 10:19:36.138: E/AndroidRuntime(21351):    at dalvik.system.NativeStart.main(Native Method)

否。 2

09-22 10:19:20.308: E/AndroidRuntime(21337): FATAL EXCEPTION: main
09-22 10:19:20.308: E/AndroidRuntime(21337): java.lang.OutOfMemoryError: [memory exhausted]
09-22 10:19:20.308: E/AndroidRuntime(21337):    at dalvik.system.NativeStart.main(Native Method)
09-22 10:19:20.318: E/System(21337): Uncaught exception thrown by finalizer
09-22 10:19:20.378: E/System(21337): java.lang.IllegalStateException: Don't have database lock!
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
09-22 10:19:20.378: E/System(21337):    at android.util.LruCache.trimToSize(LruCache.java:197)
09-22 10:19:20.378: E/System(21337):    at android.util.LruCache.evictAll(LruCache.java:285)
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
09-22 10:19:20.378: E/System(21337):    at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
09-22 10:19:20.378: E/System(21337):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
09-22 10:19:20.378: E/System(21337):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
09-22 10:19:20.378: E/System(21337):    at java.lang.Thread.run(Thread.java:856)

否。 3

09-22 10:19:20.308: E/SQLiteDatabase(21337): close() was never explicitly called on database '/data/data/challenge.main/databases/DBCHALLENGES' 
09-22 10:19:20.308: E/SQLiteDatabase(21337): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at challenge.sql.CustomDataSource.open(CustomDataSource.java:33)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at challenge.main.OverviewActivity.updateOverview(OverviewActivity.java:38)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at challenge.main.OverviewActivity.onCreate(OverviewActivity.java:29)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.Activity.performCreate(Activity.java:4465)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.os.Looper.loop(Looper.java:137)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at android.app.ActivityThread.main(ActivityThread.java:4340)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    09-22 10:19:20.308: E/SQLiteDatabase(21337):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-22 10:19:20.308: E/SQLiteDatabase(21337):    at dalvik.system.NativeStart.main(Native Method)

现在的问题是 - 我认为 - 是由getAllEntries()

中此方法中的OverviewActivity引起的
public void updateOverview(){
        datasource = new CustomDataSource(this);
        datasource.open();
        Game game = datasource.getSpecificGame(challengeName);
        entries = datasource.getAllEntries();
        datasource.close();

            (...)
}

这是CustomDataSource

中的代码
public Vector<FootballEntry> getAllEntries(){
    Vector<FootballEntry> entries = new Vector<FootballEntry>();

    Cursor cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL, allColumns2, null, null, null, null, null);
    cursor.moveToFirst();

    while(!cursor.isAfterLast()){
        FootballEntry entry = cursorToEntry(cursor);
        entries.add(entry);
        cursor.moveToNext();
    }
    cursor.close();
    return entries;
}

private FootballEntry cursorToEntry(Cursor cursor){
    cursor.moveToFirst();
    FootballEntry game = new FootballEntry();
    game.setID(cursor.getLong(0));
    game.setDate(cursor.getString(1));
    game.setHome(cursor.getInt(2));
    game.setPlatform(cursor.getString(3));
    game.setTeam1(cursor.getString(4));
    game.setStars1(cursor.getDouble(5));
    game.setTeam2(cursor.getString(6));
    game.setStars2(cursor.getDouble(7));
    game.setGoals1(cursor.getInt(8));
    game.setGoals2(cursor.getInt(9));
    game.setFinished(cursor.getInt(10));
    return game;
 }

这里是CustomDataSource的构造函数以及方法open()close()

public CustomDataSource(Context context){
        dbHelper = new CustomSQLiteHelper(context);
}

public void open() throws SQLException{
        database = dbHelper.getWritableDatabase();
}

public void close(){
        dbHelper.close();
}

注意:我已经阅读了几次,我的CustomSQLiteHelper应该扩展SQLiteOpenHelper,就是这种情况。

我不完全理解为什么会抛出这些错误。每次我想使用数据库时,我都会创建一个新的CustomDataSource,致电open(),继续工作,然后致电close()。这有什么问题?首先我想,错误是由open()中的put onResume()close()我的活动onClose()方法引起的,但删除它们不会导致任何差异。此外,每个活动我只创建一个CustomDataSource的实例,在使用它之后,我关闭它。

1 个答案:

答案 0 :(得分:6)

请勿在方法 cursorToEntry 中调用cursor.moveToFirst();,因为它会将您的请求变为无限循环。因此,即使您从数据库中获取了两条记录,您当前的逻辑也会继续创建这两条记录的对象,直到设备内存不足为止。