在内存中加载大字符串时出现问题

时间:2011-09-11 14:08:32

标签: android string memory-management

我在内存中加载大字符串时遇到问题。我有一个返回字符串的WebService。有时这个字符串的大小大约是4MB。

我的代码:

... 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
envelope.dotNet = true;

envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url);

androidHttpTransport.call(soapAction, envelope);

当字符串的大小很大时,程序会在结束时意外停止,没有例外。

我该怎么做才能解决这个问题?


修改 错误日志:

09-12 09:20:19.790: INFO/dalvikvm-heap(3515): Grow heap (frag case) to 12.845MB for 3319472-byte allocation
09-12 09:20:19.850: DEBUG/dalvikvm(3515): GC_CONCURRENT freed 0K, 8% free 13093K/14151K, paused 3ms+2ms
09-12 09:20:28.510: DEBUG/PowerManagerService(136): @PowerManagement: Auto-dim timeout
09-12 09:20:28.510: DEBUG/PowerManagerService(136): @PowerManagement: Screen Bright {true -> false}
09-12 09:20:38.330: DEBUG/dalvikvm(3515): GC_FOR_ALLOC freed 2179K, 23% free 10932K/14151K, paused 42ms
09-12 09:20:38.340: INFO/dalvikvm-heap(3515): Grow heap (frag case) to 15.484MB for 4979204-byte allocation
09-12 09:20:38.390: DEBUG/dalvikvm(3515): GC_CONCURRENT freed 0K, 17% free 15794K/19015K, paused 2ms+3ms
09-12 09:20:59.920: DEBUG/DockManagerService(136): Display mobile dock notificaiton, level:5 status:4
09-12 09:20:59.920: DEBUG/BatteryService(136): level:99 scale:100 status:2 health:2 present:true dock_status:4 dock_level:5 voltage: 8 temperature: 270 technology: Li-ion AC powered:true USB powered:false icon:17302732 invalid charger:0
09-12 09:20:59.930: INFO/fdhttpd(3345): battery level:99 plugged:1
09-12 09:20:59.930: DEBUG/PowerUI(184): closing low battery warning: level=99
09-12 09:21:05.070: DEBUG/dalvikvm(223): GC_EXPLICIT freed 1K, 33% free 21283K/31431K, paused 4ms+3ms
09-12 09:21:12.440: DEBUG/dalvikvm(3515): GC_FOR_ALLOC freed 3267K, 34% free 12553K/19015K, paused 31ms
09-12 09:21:12.450: INFO/dalvikvm-heap(3515): Grow heap (frag case) to 19.441MB for 7468802-byte allocation
09-12 09:21:12.560: DEBUG/dalvikvm(3515): GC_CONCURRENT freed 0K, 25% free 19846K/26311K, paused 2ms+2ms
09-12 09:21:59.380: DEBUG/dalvikvm(3515): GC_FOR_ALLOC freed 4902K, 44% free 14984K/26311K, paused 37ms
09-12 09:21:59.400: INFO/dalvikvm-heap(3515): Grow heap (frag case) to 25.376MB for 11203198-byte allocation
09-12 09:21:59.490: DEBUG/dalvikvm(3515): GC_FOR_ALLOC freed 0K, 31% free 25925K/37255K, paused 26ms
09-12 09:21:59.540: DEBUG/dalvikvm(3515): GC_CONCURRENT freed 7293K, 50% free 18631K/37255K, paused 2ms+3ms
09-12 09:22:39.230: ERROR/MPL-accel(136): ../../accel.c|AccelGetData|144 returning 146
09-12 09:22:39.230: ERROR/MPL-fifo(136): ../../mlFIFO.c|readAndProcessFIFO|569 returning 146
09-12 09:22:39.230: ERROR/MPL-ml(136): ../../ml.c|MLUpdateData|763 returning 146
09-12 09:22:39.230: ERROR/Sensors(136): MLUpdateData error (code 146)
09-12 09:23:00.230: DEBUG/BatteryService(136): level:99 scale:100 status:2 health:2 present:true dock_status:4 dock_level:4 voltage: 8 temperature: 269 technology: Li-ion AC powered:true USB powered:false icon:17302732 invalid charger:0
09-12 09:23:00.240: DEBUG/DockManagerService(136): Display mobile dock notificaiton, level:4 status:4
09-12 09:23:00.240: INFO/fdhttpd(3345): battery level:99 plugged:1
09-12 09:23:00.240: DEBUG/PowerUI(184): closing low battery warning: level=99
09-12 09:23:05.300: DEBUG/dalvikvm(223): GC_EXPLICIT freed 1K, 33% free 21283K/31431K, paused 8ms+3ms
09-12 09:23:08.620: DEBUG/dalvikvm(3515): GC_FOR_ALLOC freed 59K, 50% free 18631K/37255K, paused 33ms
09-12 09:23:08.620: INFO/dalvikvm-heap(3515): Forcing collection of SoftReferences for 16804792-byte allocation
09-12 09:23:08.650: DEBUG/dalvikvm(3515): GC_BEFORE_OOM freed 8K, 51% free 18622K/37255K, paused 28ms
09-12 09:23:08.650: ERROR/dalvikvm-heap(3515): Out of memory on a 16804792-byte allocation.
09-12 09:23:08.650: INFO/dalvikvm(3515): "Thread-11" prio=5 tid=10 RUNNABLE
09-12 09:23:08.650: INFO/dalvikvm(3515):   | group="main" sCount=0 dsCount=0 obj=0x4085c4b0 self=0x172650
09-12 09:23:08.650: INFO/dalvikvm(3515):   | sysTid=3558 nice=0 sched=0/0 cgrp=default handle=1831224
09-12 09:23:08.650: INFO/dalvikvm(3515):   | schedstat=( 4096840000 1017805000 13067 ) utm=332 stm=77 core=1
09-12 09:23:08.650: INFO/dalvikvm(3515):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~96)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:126)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at java.lang.StringBuilder.append(StringBuilder.java:272)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.kxml2.io.KXmlParser.readValue(KXmlParser.java:1338)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.kxml2.io.KXmlParser.next(KXmlParser.java:390)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:404)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:271)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:404)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:131)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.transport.Transport.parseResponse(Transport.java:90)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.mabna.order.ui.ActToolDataExchange.threadGetFiles(ActToolDataExchange.java:185)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.mabna.order.ui.ActToolDataExchange.access$3(ActToolDataExchange.java:145)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.mabna.order.ui.ActToolDataExchange$6.run(ActToolDataExchange.java:134)
09-12 09:23:08.650: INFO/dalvikvm(3515):   at org.mabna.order.utils.Utilities$5.run(Utilities.java:718)
09-12 09:23:08.650: WARN/dalvikvm(3515): threadid=10: thread exiting with uncaught exception (group=0x401c4760)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515): FATAL EXCEPTION: Thread-11
09-12 09:23:08.660: ERROR/AndroidRuntime(3515): java.lang.OutOfMemoryError
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:96)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:126)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at java.lang.StringBuilder.append(StringBuilder.java:272)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at org.kxml2.io.KXmlParser.readValue(KXmlParser.java:1338)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:390)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
09-12 09:23:08.660: ERROR/AndroidRuntime(3515):     at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:404)

1 个答案:

答案 0 :(得分:0)

不是安卓大师,所以我可能会得到一些投票给这个答案。

您说您有一个返回字符串的Web服务,所以基本上您正在下载4mb的数据并加载它。你的问题可能是你的Android设备可以下载/上传数据的速度。 您是否尝试在真实设备上运行此操作并观看LogCat窗口?可能是网络请求导致主线程超时。

要验证这一点,您可以尝试使用可能从服务器获得的示例响应手动设置变量。 String str = "abcd... 如果这会使您的应用程序崩溃,那么我不确定。但如果可行,那么很可能是您的网络请求导致超时。

要解决网络请求超时问题,您实际应该做的是在单独的线程中运行网络请求。可以在Android Dev网站上找到示例。