サーバーから大きな文字列をロード中にメモリ不足エラーが発生しました。
これは私のコードです:
public String sendRequest(HttpRequest httpRequest){
Log.d(TAG, "Sending HTTP request to the server");
Log.d("memory", "memory size is:"+Runtime.getRuntime().maxMemory());
String response = null;
HttpHost host = new HttpHost(context.getString(R.string.HOST), -1, context.getString(R.string.SCHEME));
try {
HttpResponse httpResponse = httpClient.execute(host, httpRequest);
int status=httpResponse.getStatusLine().getStatusCode();
if (status==200) {
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity != null) {
InputStream stream = responseEntity.getContent();
response = convertStreamToString(stream);
stream.close();
}
}
else{
response="failed:"+httpResponse.getStatusLine().toString();
}
} catch (Exception e) {
Log.v(TAG,"Request sending failed: ", e);
}
Log.d(TAG, "Completed, Sending HTTP request to the server");
return response;
}
/**
* Convert stream to string.
*
* @param is the input Stream.
* @return the string
*/
private 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 + NEW_LINE);
}
reader.close();
} catch (IOException e) {
//do nothing.
} finally {
try {
reader.close();
} catch (IOException e) {
//do nothing.
}
}
reader=null;
return sb.toString();
}
この行で例外が発生しています:
while ((line = reader.readLine()) != null) {
sb.append(line + NEW_LINE);
}
これは私のログキャットの詳細です:
10-10 15:01:50.270: D/dalvikvm(632): GC_FOR_ALLOC freed <1K, 37% free 24085K/38215K, paused 225ms
10-10 15:01:51.032: I/dalvikvm-heap(632): Grow heap (frag case) to 31.845MB for 8628650-byte allocation
10-10 15:01:51.929: D/dalvikvm(632): GC_CONCURRENT freed <1K, 15% free 32511K/38215K, paused 10ms+32ms
10-10 15:01:52.192: D/dalvikvm(632): GC_FOR_ALLOC freed 0K, 15% free 32511K/38215K, paused 226ms
10-10 15:01:52.192: I/dalvikvm-heap(632): Forcing collection of SoftReferences for 12942970-byte allocation
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:53.219: E/AndroidRuntime(632): FATAL EXCEPTION: IntentService[SurveyQuestionService]
10-10 15:01:53.219: E/AndroidRuntime(632): java.lang.OutOfMemoryError
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.StringBuilder.append(StringBuilder.java:216)
10-10 15:01:53.219: E/AndroidRuntime(632): at com.handigital.products.agilesurveys.services.SurveyQuestionService.processResponse(SurveyQuestionService.java:34)
10-10 15:01:53.219: E/AndroidRuntime(632): at com.handigital.products.agilesurveys.services.AbstractIntentService.onHandleIntent(AbstractIntentService.java:44)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.Looper.loop(Looper.java:137)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.HandlerThread.run(HandlerThread.java:60)
誰でもこれについて私を助けてください。
前もって感謝します。