简体   繁体   中英

Java Out of Memory Exception While Reading Database

I am trying to retrieve data from a database. However, while reading, my cursor threw a Java Out of Memory Exception. Looking at the stack trace, it had to do with a string builder. I thought that moving the code to a thread would work, but I still had the exception. Here is the code that is not working:

 public Object[] getData() {
    CustomRunnable runnable = new CustomRunnable();
    Thread runThread = new Thread(runnable);
    runThread.start();
    while ((runThread.getState() != Thread.State.TERMINATED)) {
    }
    return runnable.getDBlist().toArray();
}

public class CustomRunnable implements Runnable {

    List<String> dbList;
    public boolean done = false;

    @Override
    public void run() {
        dbList = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_ITEMS + " WHERE 1";
        //Cursor code
        Cursor c = sqLiteDatabase.rawQuery(query, null);
        c.moveToFirst();
        String dbString = "";
        while (!c.isAfterLast()) {
            if (c.getString(c.getColumnIndex(COLUMN_ITEMNAME)) != null) {
                dbString += c.getString(c.getColumnIndex(COLUMN_ITEMNAME));
            }
        /*
        if(c.getString(c.getColumnIndex(COLUMN_COST))!=null){
            dbString+=c.getString(c.getColumnIndex(COLUMN_COST));
        }
        */
            dbList.add(dbString);
        }
        c.close();
        sqLiteDatabase.close();
        done = true;
    }

    public List<String> getDBlist() {
        return dbList;
    }
}

Here is my stack trace:

java.lang.OutOfMemoryError: Failed to allocate a 83410 byte allocation with 71348 free bytes and 69KB until OOM
                                                                    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
                                                                    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)
                                                                    at java.lang.StringBuilder.append(StringBuilder.java:216)
                                                                    at com.daita.ereciept.DBHandler.getData(DBHandler.java:62)
                                                                    at com.daita.ereciept.MainActivity$1.run(MainActivity.java:37)
                                                                    at java.lang.Thread.run(Thread.java:818)

You need to troubleshoot your JVM heap size as first option. If you are sure you are not wrong.

By wrong I mean... You know it's not much data to cause oom. And you need this much data in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM