Valentino Ru
Valentino Ru

Reputation: 5052

Android: java.lang.OutOfMemoryError though very small database

I'm developping an application to store some bets made between two people.

Now I'm pretty much new into this and especially with SQLite. The problem is, I'm getting one of following ErrorMessages all the time, despite there are only 2 or 3 entries in my database.

No. 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)

No. 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)

No. 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)

Now the problem is - as I think - caused by the getAllEntries() in this method in my OverviewActivity class

public void updateOverview(){
        datasource = new CustomDataSource(this);
        datasource.open();
        Game game = datasource.getSpecificGame(challengeName);
        entries = datasource.getAllEntries();
        datasource.close();

            (...)
}

This is the Code in the CustomDataSource class

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;
 }

And here the constructor of CustomDataSource and the methods open() and close()

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

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

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

Note: I've read a few times, that my CustomSQLiteHelper should extend SQLiteOpenHelper, that is the case.

I do not exactly understand why this errors are thrown. Everytime I want to do work with the database, I create a new CustomDataSource, call open(), do work, and call close(). What's the problem here? First I thought, the error is caused by puttin open() in the onResume() and close() in the onClose() method of my Activity, but removing them do not causes any differences. Also per activity I create only one instance of CustomDataSource, and after working with it, I close it.

Upvotes: 2

Views: 1771

Answers (1)

waqaslam
waqaslam

Reputation: 68177

Don't call cursor.moveToFirst(); in method cursorToEntry because it turns your request into an infinite loop. So even if you have two records fetched from database, your current logic will keep on creating objects of those two records till the device runs out of memory.

Upvotes: 6

Related Questions