2011-08-22 41 views
0

我正在嘗試使用Web服務下載圖像。實際的圖像流作爲響應字符串的一部分提供。我使用AsyncTask在後臺進程中運行此請求。線程開始接收實際的字節流後,內存堆增加。我正在把日誌放在下面。接收增加堆大小的HTML響應?

08-22 12:26:45.131: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 2322 objects/190832 bytes in 39ms 
08-22 12:26:47.231: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 1352 objects/409784 bytes in 29ms 
08-22 12:26:47.381: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 625 objects/219384 bytes in 31ms 
08-22 12:26:47.621: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 1036 objects/336544 bytes in 29ms 
08-22 12:26:47.621: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 3.413MB for 334198-byte allocation 
08-22 12:26:47.671: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 42ms 
08-22 12:26:47.941: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 1651 objects/506808 bytes in 31ms 
08-22 12:26:47.951: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 3.672MB for 501292-byte allocation 
08-22 12:26:47.991: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 39ms 
08-22 12:26:49.701: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 2255 objects/749472 bytes in 28ms 
08-22 12:26:49.701: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 4.071MB for 751934-byte allocation 
08-22 12:26:49.741: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 37ms 
08-22 12:26:51.561: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 2731 objects/1027912 bytes in 28ms 
08-22 12:26:51.711: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 587 objects/103544 bytes in 31ms 
08-22 12:26:51.711: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 4.669MB for 1127896-byte allocation 
08-22 12:26:51.761: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 43ms 
08-22 12:26:52.801: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 3872 objects/1486920 bytes in 31ms 
08-22 12:26:53.011: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 963 objects/196184 bytes in 30ms 
08-22 12:26:53.011: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 5.565MB for 1691840-byte allocation 
08-22 12:26:53.051: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 40ms 
08-22 12:26:54.291: VERBOSE/AlarmManager(103): Alarm triggering: Alarm{4669d808 type 0 com.facebook.katana} 
08-22 12:26:54.561: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 5670 objects/2174152 bytes in 28ms 
08-22 12:26:54.656: ERROR/ApiMethod.onHttpOperationComplete(586): FacebookApiException: 102/The session has been invalidated because the user has changed the password. 
08-22 12:26:54.691: INFO/LSState(103): EventReceiver:android.intent.action.NOTIFICATION_UPDATE 
08-22 12:26:54.711: VERBOSE/AlarmManager(103): Adding Alarm{46661060 type 0 com.facebook.katana} Aug 22 12:27:54 pm 
08-22 12:26:55.151: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 1963 objects/365552 bytes in 45ms 
08-22 12:26:55.161: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 6.909MB for 2537756-byte allocation 
08-22 12:26:55.201: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 39ms 
08-22 12:26:56.961: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 8165 objects/3209680 bytes in 35ms 
08-22 12:26:57.601: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 3189 objects/596336 bytes in 40ms 
08-22 12:26:57.611: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 8.927MB for 3806630-byte allocation 
08-22 12:26:57.651: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 38ms 
08-22 12:26:58.461: VERBOSE/AlarmManager(103): Alarm triggering: Alarm{46935248 type 0 com.facebook.katana} 
08-22 12:26:58.851: ERROR/ApiMethod.onHttpOperationComplete(586): FacebookApiException: 102/The session has been invalidated because the user has changed the password. 
08-22 12:26:58.911: INFO/LSState(103): EventReceiver:android.intent.action.NOTIFICATION_UPDATE 
08-22 12:26:58.931: VERBOSE/AlarmManager(103): Adding Alarm{469006d0 type 0 com.facebook.katana} Aug 22 12:27:28 pm 
08-22 12:27:00.041: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 11558 objects/4635672 bytes in 72ms 
08-22 12:27:01.021: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 5947 objects/1072128 bytes in 30ms 
08-22 12:27:01.041: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 11.951MB for 5709940-byte allocation 
08-22 12:27:01.111: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 64ms 
08-22 12:27:06.171: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 10917 objects/5903536 bytes in 30ms 
08-22 12:27:08.861: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 10522 objects/1912024 bytes in 30ms 
08-22 12:27:08.881: INFO/dalvikvm-heap(31088): Grow heap (frag case) to 13.472MB for 5401196-byte allocation 
08-22 12:27:08.941: DEBUG/dalvikvm(31088): GC_FOR_MALLOC freed 0 objects/0 bytes in 61ms 

在第五行ü可以看到堆大小爲3.413 MB和第二 最後一行它的增長達到13.472MB。

這是我用來獲取asyncTask類中的數據的代碼。

@Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      RestClient client = new RestClient(CALL_URL); 
      client.AddParam("token", params[0]); 

      client.AddParam("filename", params[3]); 
      try { 
       client.Execute(RestClient.RequestMethod.GET); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      String response = client.getResponse(); 
      return response;  
     } 

這些都是RESTClient實現類,實際上與HttpRequest的

private void executeRequest(HttpUriRequest request, String url) 
    { 
     HttpClient client = new DefaultHttpClient(); 

     HttpResponse httpResponse; 

     try { 
      httpResponse = client.execute(request); 
      responseCode = httpResponse.getStatusLine().getStatusCode(); 
      message = httpResponse.getStatusLine().getReasonPhrase(); 

      HttpEntity entity = httpResponse.getEntity(); 

      if (entity != null) { 

       InputStream instream = entity.getContent(); 
       response = convertStreamToString(instream); 

       // Closing the input stream will trigger connection release 
       instream.close(); 
      } 

     } catch (ClientProtocolException e) { 
      client.getConnectionManager().shutdown(); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      client.getConnectionManager().shutdown(); 
      e.printStackTrace(); 
     } 
    } 

    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(); 
    } 

可能是什麼原因可能涉及內部的方法。我如何有效地做到這一點在Android中佔用這麼多內存?

回答

0

我不熟悉Android,但可以將內容存儲在文件系統而不是內存中。