Autore Topic: Json e outofmemory  (Letto 780 volte)

Offline DX89B

  • Utente junior
  • **
  • Post: 121
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
  • Play Store ID:
    Daniele Bonadiman
  • Sistema operativo:
    Mac OS X 10.6
Json e outofmemory
« il: 06 Settembre 2010, 14:58:35 CEST »
0
 eccomi con l'ennesimo problema da risolvere :)

allora una parte della mia applicazione è interrogare un server che mi invii un file json contenente il risultato di una ricerca.
fino a qui nessun problema,ma se cerco una stringa vuota, quindi tutto,succede questo


09-06 14:40:48.894: INFO/logrest(26655): HTTP/1.1 200 OK
09-06 14:40:48.904: INFO/global(26655): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
09-06 14:40:48.974: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 4000 objects / 306024 bytes in 38ms
09-06 14:40:49.034: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 5389 objects / 849344 bytes in 33ms
09-06 14:40:49.114: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 5776 objects / 458552 bytes in 41ms
09-06 14:40:49.164: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 4081 objects / 426224 bytes in 29ms
09-06 14:40:49.244: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 7397 objects / 415704 bytes in 40ms
09-06 14:40:49.244: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 3.708MB for 441514-byte allocation
09-06 14:40:49.284: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 38ms
09-06 14:40:49.374: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 9226 objects / 819192 bytes in 51ms
09-06 14:40:49.414: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 3044 objects / 172008 bytes in 29ms
09-06 14:40:49.414: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 4.059MB for 662266-byte allocation
09-06 14:40:49.454: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 40ms
09-06 14:40:49.524: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 9334 objects / 965640 bytes in 31ms
09-06 14:40:49.614: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 9252 objects / 524352 bytes in 38ms
09-06 14:40:49.634: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 48 objects / 2448 bytes in 29ms
09-06 14:40:49.644: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 4.585MB for 993394-byte allocation
09-06 14:40:49.684: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 38ms
09-06 14:40:49.774: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 12480 objects / 1367480 bytes in 41ms
09-06 14:40:49.854: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 9240 objects / 524152 bytes in 50ms
09-06 14:40:49.904: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 6023 objects / 341440 bytes in 29ms
09-06 14:40:49.904: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 5.375MB for 1490086-byte allocation
09-06 14:40:49.944: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 38ms
09-06 14:40:50.054: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 17323 objects / 1974640 bytes in 42ms
09-06 14:40:50.134: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 11570 objects / 650512 bytes in 41ms
09-06 14:40:50.224: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 11338 objects / 650608 bytes in 37ms
09-06 14:40:50.254: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 1158 objects / 64872 bytes in 31ms
09-06 14:40:50.264: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 6.559MB for 2235124-byte allocation
09-06 14:40:50.304: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 38ms
09-06 14:40:50.444: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 24706 objects / 2885344 bytes in 42ms
09-06 14:40:50.534: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 16019 objects / 898824 bytes in 37ms
09-06 14:40:50.634: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 15999 objects / 898864 bytes in 37ms
09-06 14:40:50.684: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 6347 objects / 357512 bytes in 30ms
09-06 14:40:50.694: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 8.335MB for 3352682-byte allocation
09-06 14:40:50.734: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 38ms
09-06 14:40:50.924: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 35585 objects / 4251936 bytes in 41ms
09-06 14:40:51.044: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 22478 objects / 1271208 bytes in 50ms
09-06 14:40:51.164: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 22482 objects / 1271128 bytes in 41ms
09-06 14:40:51.254: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 12901 objects / 726680 bytes in 41ms
09-06 14:40:51.264: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 11.000MB for 5029018-byte allocation
09-06 14:40:51.334: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 62ms
09-06 14:40:51.534: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37005 objects / 5449816 bytes in 52ms
09-06 14:40:51.694: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 32477 objects / 1830208 bytes in 39ms
09-06 14:40:51.854: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 32355 objects / 1830224 bytes in 41ms
09-06 14:40:52.024: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 32408 objects / 1830104 bytes in 37ms
09-06 14:40:52.074: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 6235 objects / 353352 bytes in 31ms
09-06 14:40:52.084: INFO/dalvikvm-heap(26655): Grow heap (frag case) to 14.997MB for 7543522-byte allocation
09-06 14:40:52.144: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 64ms
09-06 14:40:52.344: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37056 objects / 7126104 bytes in 41ms
09-06 14:40:52.534: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37215 objects / 2097112 bytes in 57ms
09-06 14:40:52.744: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37278 objects / 2097104 bytes in 41ms
09-06 14:40:52.744: DEBUG/dalvikvm(246): GC_EXPLICIT freed 1594 objects / 215816 bytes in 196ms
09-06 14:40:52.934: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37172 objects / 2097256 bytes in 50ms
09-06 14:40:53.094: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 37180 objects / 2097112 bytes in 38ms
09-06 14:40:53.144: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 8056 objects / 449152 bytes in 30ms
09-06 14:40:53.144: INFO/dalvikvm-heap(26655): Forcing collection of SoftReferences for 7329830-byte allocation
09-06 14:40:53.174: DEBUG/dalvikvm(26655): GC_FOR_MALLOC freed 0 objects / 0 bytes in 29ms
09-06 14:40:53.174: ERROR/dalvikvm-heap(26655): Out of memory on a 7329830-byte allocation.
09-06 14:40:53.174: INFO/dalvikvm(26655): "AsyncTask #4" prio=5 tid=10 RUNNABLE
09-06 14:40:53.174: INFO/dalvikvm(26655):   | group="main" sCount=0 dsCount=0 s=N obj=0x46289310 self=0x2d1650
09-06 14:40:53.174: INFO/dalvikvm(26655):   | sysTid=26669 nice=10 sched=0/0 cgrp=default handle=2955152
09-06 14:40:53.174: INFO/dalvikvm(26655):   | schedstat=( 3002105706 1169433598 4299 )
09-06 14:40:53.174: INFO/dalvikvm(26655):   at java.lang.String.<init>(String.java:~468)
09-06 14:40:53.174: INFO/dalvikvm(26655):   at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:659)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.lang.StringBuilder.toString(StringBuilder.java:664)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at org.liquidjournal.RestClient.convertStreamToString(RestClient.java:54)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at org.liquidjournal.RestClient.connect(RestClient.java:90)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at org.liquidjournal.Search$Task.doInBackground(Search.java:134)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at org.liquidjournal.Search$Task.doInBackground(Search.java:1)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-06 14:40:53.184: INFO/dalvikvm(26655):   at java.lang.Thread.run(Thread.java:1102)
09-06 14:40:53.184: ERROR/dalvikvm(26655): Out of memory: Heap Size=18183KB, Allocated=10126KB, Bitmap Size=0KB

avete idea come è possibile evitare ciò o devo proprio disabilitare la ricerca per tutto, quello che temo è che col crescere del progetto anche una semplice ricerca mi possa far crashare l'applicazione.

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +507
    • Github
    • Google+
    • rciovati
    • Mostra profilo
Re:Json e outofmemory
« Risposta #1 il: 06 Settembre 2010, 15:02:58 CEST »
0
Ti devi ricordare che stai lavorando in un ambito dove la memoria è estremamente limitata percui dovrei studiare dei modi per usarne il meno possibile. Ad esempio nel tuo caso potresti pensare a un meccanismo che ti limita i risultati massimi a 10 o 20 oppure a un meccanismo di "paginazione" dei risultati.

Offline DX89B

  • Utente junior
  • **
  • Post: 121
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
  • Play Store ID:
    Daniele Bonadiman
  • Sistema operativo:
    Mac OS X 10.6
Re:Json e outofmemory
« Risposta #2 il: 06 Settembre 2010, 15:06:53 CEST »
0
non so come lavora bene json ma per poter usare i dati non devo ricevere tutto il contenuto?

quindi se devo fare una paginazione o qualcosa del genere devo partire dal server ,o sbaglio, in modo che possa usare query per ottenere gli oggetti a blocchi tipo
 da 1 a 30
da 31 a 60

ecc

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +507
    • Github
    • Google+
    • rciovati
    • Mostra profilo
Re:Json e outofmemory
« Risposta #3 il: 06 Settembre 2010, 15:08:00 CEST »
0
Esatto.

Offline DX89B

  • Utente junior
  • **
  • Post: 121
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
  • Play Store ID:
    Daniele Bonadiman
  • Sistema operativo:
    Mac OS X 10.6
Re:Json e outofmemory
« Risposta #4 il: 06 Settembre 2010, 15:18:56 CEST »
0
ok allora lo diamo per risolto parlerò con chi getisce il server prima di buttare l'applicazione :)

in ogni caso sai come posso catturare l'eccezione per poter bloccare il salvataggio del file per evitare almeno per ora il crash?
questo è il Client Http
Codice (Java): [Seleziona]
public class RestClient {
 
    private static String convertStreamToString(InputStream is) {
     
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
 
   
    public static JSONObject connect(String url)
    {
 
        HttpClient httpclient = new DefaultHttpClient();
 
       
        HttpGet httpget = new HttpGet(url);
 
       
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
           
               
         
            Log.i("logrest",response.getStatusLine().toString());
           
 
           
            HttpEntity entity = response.getEntity();
         
 
            if (entity != null&&response.getStatusLine().getStatusCode()==200) {
               
               
 
                //il problema dovrebbe essere qua
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                Log.i("logrest",result);
 
             
                JSONObject json=new JSONObject(result);
                Log.i("logrest","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
 
               
                JSONArray nameArray=json.names();
                JSONArray valArray=json.toJSONArray(nameArray);
                for(int i=0;i<valArray.length();i++)
                {
                    Log.i("logrest","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
                            +"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
                }
 
                instream.close();
                return json;
            }
           
 
 
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
   
   
 
}