Android reading a 917KB file from web causes memory error?
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()),1024*32);
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line.replace(" ", ""));
}
rd.close();
result = sb.toString();
I am suspecting that it is the fact that too many strings are being created. How can i see wether a readline is possible and stick the lineread straight into the stringbuilder. What other way is there to do this? The file is just under 1MB
Here is the log:
09-27 12:56:07.740: ERROR/ERROR: Problem in LDataListView: refreshError 09-27 12:56:07.740: ERROR/ERROR: ------------------------------------------- 09-27 12:56:13.339: ERROR/Dhcpcd(10598): sending DHCP_REQUEST with xid 0x5a93b3de, next in 3.58 seconds 09-27 12:56:13.549: ERROR/dalvikvm-heap(12862): Out of memory on a 6064234-byte allocation. 09-27 12:56:13.549: ERROR/dalvikvm(12862): Out of memory: Heap Size=15687KB, Allocated=6704KB, Bitmap Size=8617KB 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): FATAL EXCEPTION: main 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): java.lang.OutOfMemoryError 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:117) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at java.lang.StringBuilder.append(StringBuilder.java:250) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at se.brickit.foundation.data.BrHttpClient.sendGetRequest(BrHttpClient.java:56) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at se.brickit.foundation.data.BrDataProvider.(BrDataProvider.java:100) 09-27 12:56:13.559: ERROR/AndroidRuntime(1286开发者_如何学运维2): at se.brickit.foundation.iviews.LisaDataListView$RefreshTask.onPostExecute(LisaDataListView.java:110) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at se.brickit.foundation.iviews.LisaDataListView$RefreshTask.onPostExecute(LisaDataListView.java:1) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.os.AsyncTask.finish(AsyncTask.java:417) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.os.AsyncTask.access$300(AsyncTask.java:127) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.os.Handler.dispatchMessage(Handler.java:99) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.os.Looper.loop(Looper.java:144) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at android.app.ActivityThread.main(ActivityThread.java:4937) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at java.lang.reflect.Method.invokeNative(Native Method) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at java.lang.reflect.Method.invoke(Method.java:521) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-27 12:56:13.559: ERROR/AndroidRuntime(12862): at dalvik.system.NativeStart.main(Native Method) 09-27 12:56:14.819: ERROR/ActivityManager(101): fail to set top app changed! 09-27 12:56:48.499: ERROR/BluetoothHeadsetService(201): java.util.NoSuchElementException
Edit:::
more info:
It is a JSON string. basically a single object with an array of items. The whole feed is littered with encoded images which makes it much larger than it should be (unfortuntely it is not my feed). the iphone seem to read it, but my HTC phone does sometimes, but usually hits a memory exception on the sb append line.
I have also tried just a straight read on it eg:
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
InputStream iFile = conn.getInputStream();
result = inputStreamToString(iFile);
but same problem there too.
精彩评论