Search code examples
androidsqliteandroid-sqlite

Failed to allocate a 83070912 byte allocation with 25165824 free bytes and 34MB until OOM, max allowed footprint 525798800, growth limit 536870912


i am really stuck. I am familiar in Java and Android development but i do not have any experience with databases.

public ArrayList<String> getIstilah(int dicType){
    String tableName = getTableName(dicType);
    String q = "SELECT * FROM "+tableName;
    Cursor result = sDB.rawQuery(q, null);

    ArrayList<String> source = new ArrayList<>();
    while (result.moveToFirst()){
        source.add(result.getString(result.getColumnIndex(COL_JUDUL)));// line 97
    }
    return source;
}

2019-10-31 16:36:16.881 2267-2267/? E/Zygote: isWhitelistProcess - Process is Whitelisted 2019-10-31 16:36:16.884 2267-2267/? E/Zygote: accessInfo : 1 2019-10-31 16:36:42.843 2267-2267/id.AntBeeDev.ContohKamus E/AndroidRuntime: FATAL EXCEPTION: main Process: id.AntBeeDev.ContohKamus, PID: 2267 java.lang.OutOfMemoryError: Failed to allocate a 83070912 byte allocation with 25165824 free bytes and 34MB until OOM, max allowed footprint 525798768, growth limit 536870912 at java.util.Arrays.copyOf(Arrays.java:3139) at java.util.Arrays.copyOf(Arrays.java:3109) at java.util.ArrayList.grow(ArrayList.java:275) at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:249) at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:241) at java.util.ArrayList.add(ArrayList.java:467) at id.AntBeeDev.ContohKamus.DBHelper.getIstilah(DBHelper.java:97) at id.AntBeeDev.ContohKamus.MainActivity.onCreateOptionsMenu(MainActivity.java:105) at android.app.Activity.onCreatePanelMenu(Activity.java:3568) at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:325) at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2830) at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:455) at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:56) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7078) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)


Solution

  • You get the Cursor and move to the first row add the data to the array then move to the first row and add the data FOREVER (as long as 1 or more rows have been extracted by the query).

    You should use moveToNext rather than moveToFirst (which will move to the First row initially, as the cursor is initially positioned at before the first row (position -1)) e.g. :-

    while (result.moveToNext()){
        source.add(result.getString(result.getColumnIndex(COL_JUDUL)));// line 97
    }
    result.close(); //<<<<<<<<<< SHOULD ALWAYS CLOSE CURSORS WHEN DONE WITH THEM
    return source;