Android: SQLite cursor error above 1 MB of database size -


i developing application using sqlite database. have following code getting database values.

arraylist<string> values=new arraylist<string>();  string[] columnnames; string selectquery="select * "+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 database size more 1mb, app getting crashed. how can database values more 1 mb.?

edit1:

logcat values:

10-10 14:46:24.863: e/dalvikvm-heap(3248): out of memory on 6612888-byte allocation. 

edit2:

code using 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 value stringbuffer 4860024.

well, it's clear you're running out of memory. building string representation of database using series of immutable string objects.

string value=""; 

every time string concatenation, value=value +", \\\"" creating new string object. inefficient in terms of memory usage.

try using stringbuilder instead of string, , use append() method instead of string concatenation operator +. allow string built in single buffer (which grow needed), makes more fit in available heap memory.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -