接收HTML响应增加堆大小?

时间:2011-08-22 11:59:25

标签: java android httpclient out-of-memory heap-memory

我正在尝试使用网络服务下载图片。实际图像流作为响应字符串的一部分传递。我正在使用AsyncTask在后台进程中运行此请求。在线程开始接收实际字节流之后,内存堆正在增加。我把日志放在下面。

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中占用这么多内存来有效地做到这一点?

1 个答案:

答案 0 :(得分:0)

我不熟悉Android,但您可以将内容存储在文件系统而不是内存中。