0

I am developing application using the SQLite database. I have the following code for getting the database all the values.

ArrayList<String> values=new ArrayList<String>(); 
String[] ColumnNames;
String selectQuery="SELECT * FROM "+tableName;
Cursor cursor=sDataBase.rawQuery(selectQuery, null);
if(cursor!=null && cursor.getColumnCount()>0 && cursor.getCount()>0){
    ColumnNames=cursor.getColumnNames();
    if (cursor.moveToFirst()){
        do{
            String value="";
            for(int i=0;i<cursor.getColumnCount();i++){
                if(value!="")
                    value=value +", \\\""+ColumnNames[i]+"\\\":\\\"" + cursor.getString(i)+"\\\"";
                else
                    value= "\\\""+ ColumnNames[i] +"\\\":\\\"" + cursor.getString(i) +"\\\"";
            }
            value="{" + value + "}";
            values.add(value);
        }while(cursor.moveToNext());
    }
    cursor.close();
}

If the database size is more than 1MB, the app is getting crashed. How can I get the database values more than 1 MB.?

EDIT1:

Logcat values:

10-10 14:46:24.863: E/dalvikvm-heap(3248): Out of memory on a 6612888-byte allocation.

EDIT2:

Code using the stringbuffer

   if(cursor!=null && cursor.getColumnCount()>0 && cursor.getCount()>0)
            {
                ColumnNames=cursor.getColumnNames();
                if (cursor.moveToFirst()){
                       do{
                           StringBuffer value= new StringBuffer();
                            value.append("{");
                           for(int i=0;i<cursor.getColumnCount();i++)
                           {
                               if(value.length()>1)
                                   value.append(", \\\""+ColumnNames[i]+"\\\":\\\"" + cursor.getString(i)+"\\\"");
                               else
                                  value.append("\\\""+ ColumnNames[i] +"\\\":\\\"" + cursor.getString(i) +"\\\"");
                           }
                           value.append(value + "}");
                           values.add(value);
                       }while(cursor.moveToNext());
                    }
                    cursor.close();
            }

Length of the value StringBuffer is 4860024.

Karthick
  • 1,241
  • 5
  • 20
  • 43

1 Answers1

1

Well, it's clear that you're just running out of memory. You are building up a String representation of the database using a series of immutable String objects.

String value="";

Every time you do a string concatenation, value=value +", \\\"" you are creating a new String object. This is very inefficient in terms of memory usage.

Try using a StringBuilder instead of a String, and use its append() method instead of the string concatenation operator +. This will allow the string to be built up in a single buffer (which will grow as needed), which makes it much more likely to fit in the available heap memory.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • I am developing application, which is usually calls the native method via javascript. This method call returns the json string to the html page. That's the reason I am having the string manipulations. One more think, I have read some questions which describes, the android cursor size is 1MB. Is this right? – Karthick Oct 10 '13 at 09:53
  • Is any suggestions for this scenario? – Karthick Oct 10 '13 at 10:29
  • I don't understand what you are asking. Have you made the suggested changes? Did it solve the problem? – Graham Borland Oct 10 '13 at 10:38
  • http://stackoverflow.com/questions/5406429/cursor-size-limit-in-android-sqlitedatabase In this link, they specified the cursor size is 1MB. I am asking is this right? I need JSON values of the database values, that is the reason I used string manipulations. I am trying to generate the JSON string using the stringbuilder. – Karthick Oct 10 '13 at 10:45
  • If you post more of the stacktrace from LogCat, that might help us identify if the allocation failure is occurring inside SQLite, or with your string concatenations. In any event, please try my suggestion and let me know if it helps. – Graham Borland Oct 10 '13 at 10:54
  • Thanks for the update. I am trying your suggestions. Facing difficulties to generate the JSON string with string builder. I am getting the one line of the error in logcat. – Karthick Oct 10 '13 at 10:56
  • Using the string builder also the application gets crashed. I pasted the code in my questions. – Karthick Oct 10 '13 at 12:24
  • I am getting the stringbuffer length is 4860024. This is the root cause of the application crashe. – Karthick Oct 10 '13 at 12:40
  • actually, any decent java compiler transform a string concatenation into a stringbuilder. – njzk2 Oct 10 '13 at 12:42