3

我正在使用以下设备来测试我的应用程序:

在此处输入图像描述

我发布了一个大小约为 498467 字节 (0.48MB) 的 JSON 字符串。

当我的应用程序同步这些数据时,它会失败,toString()例如JSONObect

JSONObject jsonObj = new JSONObject();
...
ClientMyAppApi.Upload(getContext(), account.name, jsonObj.toString()); // Out of memory here

这是 LogCat 成绩单:

D/dalvikvm(21964): GC_CONCURRENT freed 690K, 12% free 10892K/12295K, paused 5ms+9ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 606K, 9% free 11196K/12295K, paused 57ms
I/dalvikvm-heap(21964): Grow heap (frag case) to 11.630MB for 662266-byte allocation
D/dalvikvm(21964): GC_FOR_ALLOC freed 431K, 13% free 11411K/12999K, paused 61ms
I/dalvikvm-heap(21964): Grow heap (frag case) to 12.157MB for 993394-byte allocation
D/dalvikvm(21964): GC_FOR_ALLOC freed 646K, 17% free 11734K/14023K, paused 63ms
I/dalvikvm-heap(21964): Grow heap (frag case) to 12.947MB for 1490086-byte allocation
D/dalvikvm(21964): GC_FOR_ALLOC freed 970K, 22% free 12220K/15495K, paused 69ms
D/AdapterSync(21964): uploadNewContent() JSON request size: 498468
D/dalvikvm(21964): GC_CONCURRENT freed 3172K, 30% free 10892K/15495K, paused 5ms+26ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 606K, 28% free 11195K/15495K, paused 56ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 431K, 27% free 11411K/15495K, paused 59ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 646K, 25% free 11734K/15495K, paused 61ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 970K, 22% free 12219K/15495K, paused 69ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 1459K, 25% free 11742K/15495K, paused 74ms
D/dalvikvm(21964): GC_CONCURRENT freed 664K, 18% free 12726K/15495K, paused 17ms+12ms
D/dalvikvm(21964): GC_CONCURRENT freed 1930K, 18% free 12723K/15495K, paused 22ms+10ms
D/dalvikvm(21964): GC_FOR_ALLOC freed 13K, 18% free 12716K/15495K, paused 70ms
I/dalvikvm-heap(21964): Forcing collection of SoftReferences for 1495472-byte allocation
D/dalvikvm(21964): GC_BEFORE_OOM freed 9K, 18% free 12707K/15495K, paused 114ms
E/dalvikvm-heap(21964): Out of memory on a 1495472-byte allocation.
I/dalvikvm(21964): "SyncAdapterThread-1" prio=5 tid=20 RUNNABLE
I/dalvikvm(21964):   | group="main" sCount=0 dsCount=0 obj=0x4101ac78 self=0x2f5198
I/dalvikvm(21964):   | sysTid=22098 nice=10 sched=0/0 cgrp=bg_non_interactive handle=3325456
I/dalvikvm(21964):   | schedstat=( 5787106417 2132935545 325 ) utm=552 stm=26 core=0
I/dalvikvm(21964):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
I/dalvikvm(21964):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
I/dalvikvm(21964):   at java.lang.StringBuilder.append(StringBuilder.java:124)
I/dalvikvm(21964):   at libcore.net.UriCodec.appendEncoded(UriCodec.java:119)
I/dalvikvm(21964):   at libcore.net.UriCodec.encode(UriCodec.java:133)
I/dalvikvm(21964):   at java.net.URLEncoder.encode(URLEncoder.java:57)
I/dalvikvm(21964):   at org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
I/dalvikvm(21964):   at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
I/dalvikvm(21964):   at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:57)
I/dalvikvm(21964):   at com.myapp.utils.NetworkUtils.makeHttpRequestWithParams(NetworkUtils.java:177)
I/dalvikvm(21964):   at com.myapp.client.ClientMyAppApi.MakeRequest(ClientMyAppApi.java:208)
I/dalvikvm(21964):   at com.myapp.client.ClientMyAppApi.Upload(ClientMyAppApi.java:395)
I/dalvikvm(21964):   at com.myapp.syncadapter.AdapterSync.uploadNewContent(AdapterSync.java:744)
I/dalvikvm(21964):   at com.myapp.syncadapter.AdapterSync.onPerformSync(AdapterSync.java:120)
I/dalvikvm(21964):   at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:247)
W/dalvikvm(21964): threadid=20: thread exiting with uncaught exception (group=0x409c01f8)
E/AndroidRuntime(21964): FATAL EXCEPTION: SyncAdapterThread-1
E/AndroidRuntime(21964): java.lang.OutOfMemoryError
E/AndroidRuntime(21964):    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
E/AndroidRuntime(21964):    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
E/AndroidRuntime(21964):    at java.lang.StringBuilder.append(StringBuilder.java:124)
E/AndroidRuntime(21964):    at libcore.net.UriCodec.appendEncoded(UriCodec.java:119)
E/AndroidRuntime(21964):    at libcore.net.UriCodec.encode(UriCodec.java:133)
E/AndroidRuntime(21964):    at java.net.URLEncoder.encode(URLEncoder.java:57)
E/AndroidRuntime(21964):    at org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
E/AndroidRuntime(21964):    at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
E/AndroidRuntime(21964):    at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:57)
E/AndroidRuntime(21964):    at com.myapp.utils.NetworkUtils.makeHttpRequestWithParams(NetworkUtils.java:177)
E/AndroidRuntime(21964):    at com.myapp.client.ClientMyAppApi.MakeRequest(ClientMyAppApi.java:208)
E/AndroidRuntime(21964):    at com.myapp.client.ClientMyAppApi.Upload(ClientMyAppApi.java:395)
E/AndroidRuntime(21964):    at com.myapp.syncadapter.AdapterSync.uploadNewContent(AdapterSync.java:744)
E/AndroidRuntime(21964):    at com.myapp.syncadapter.AdapterSync.onPerformSync(AdapterSync.java:120)
E/AndroidRuntime(21964):    at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:247)

解决此内存不足问题的最佳方法是什么以及如何解决?

4

3 回答 3

1

虽然这不是一个非常“优雅”的解决方案,但您可以尝试添加android:largeHeap="true"到清单中。尝试使用它运行,但我不建议将它留在那里,因为它可能会导致一些性能问题。
也就是说,如果它不能解决内存不足的问题,它可能在 JSON 或代码本身的某个地方。

于 2013-03-11T19:45:30.017 回答
0

您可以尝试Jackson 采用的流式处理方法:它似乎允许增量写入,而不会产生库存 Android JSON 库所遭受的内存开销,该库需要在将整个 JSON 对象String写入连接流之前以形式序列化整个 JSON 对象。

于 2013-03-11T19:56:40.057 回答
-3

我检查了我的 AVD 配置。我有相同的 RAM 量。但是,我有更大的堆大小,比如 48 M。我认为,值得一试。

祝你好运。

于 2013-03-11T19:43:14.470 回答