[英]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. 但是,在阅读时,我的光标抛出了Java内存不足异常。 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. 您首先需要对JVM堆大小进行故障排除。 If you are sure you are not wrong. 如果您确定自己没有错。
By wrong I mean... You know it's not much data to cause oom. 错误的意思是...您知道导致oom的数据不足。 And you need this much data in memory. 您需要在内存中存储大量数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.