HttpClient.execute抛出的OutOfMemoryError [英] HttpClient.execute throwing OutOfMemoryError

查看:1040
本文介绍了HttpClient.execute抛出的OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经是发布的JSONObject 作为一个实体使用 ByteArrayEntity 对象Android应用程序。在这里,它的样子:

I've an Android application that posting JSONObject as a entity by using ByteArrayEntity object. Here how it looks like:

post.setEntity(new ByteArrayEntity(entity.getBytes("UTF-8")));
result = client.execute(post, handler);

实体是字符串。处理器是 ResponseHandler的<字符串> 和客户端是一个的HttpClient 。这是运作良好的模拟器和一些设备。但是,有时候我得到的OutOfMemoryError在执行上 X10i的(又名XPERIA)。

Entity is a String. Handler is ResponseHandler<String> and client is a HttpClient. That's working well on emulator and on some devices. But, sometimes I'm getting OutOfMemoryError while executing on x10i (also known as XPERIA).

下面是堆栈:

java.lang.OutOfMemoryError
at org.apache.http.impl.io.AbstractSessionInputBuffer.init(AbstractSessionInputBuffer.java:79)
at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:93)
at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:171)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:678)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:652)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at com.test.application.api.Request.post(Request.java:102)
at com.test.application.api.API.getResult(API.java:123)
at com.test.application.api.APITask.doInBackground(APITask.java:127)
at com.test.application.api.APITask.doInBackground(APITask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)

正如我所说的,我只是张贴的JSONObject为一个字符串。它可以为至多200字符。怎么了?

As I said, I'm just posting a JSONObject as a String. It may be 200 character at most. What's wrong?

推荐答案

Android设备有每个进程的内存限制。默认默认为24MB,但有些设备有一个较低的数值,例如16MB或者更低。该X10i的似乎有384MB RAM,这是低一个现代的Andr​​oid设备(1GB现在的标准),这可以将额外的限制。

Android devices have a per-process memory limit. The default default is 24MB, but some devices have a lower value, eg 16MB or perhaps lower. The X10i appears to have 384MB RAM, which is low for a modern Android device (1GB is now standard), and this may place additional constraints.

我建议:

  1. 首先检查每个进程限制您的设备上。使用<一个href="http://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass%28%29">ActivityManager.getMemoryClass()检查一下。
  2. 看看你的实际内存使用量使用 DDMS
  1. First check the per-process limit on your device. Use ActivityManager.getMemoryClass() to check.
  2. Look at your actual memory usage using DDMS.

您可能会发现,只要你正在此调用之前,你已经在内存的上限; HttpClient的和呼叫的初始化可能需要您刚刚超过了极限。如果OOM发生在第一的调用这会更容易。

You may find that, just before you are making this call, you are already at the upper limit of memory; the initialisation of the HTTPClient and the call might take you just over the limit. This will be more likely if the OOM occurs on the first call.

如果出现这种情况只是偶尔出现,或呼叫次数后,你可能有内存泄漏。 DDMS将帮助您使用获得拨款功能跟踪下来。

If this happens only sporadically, or after a number of calls, you might have a memory leak. DDMS will help you track that down using the get allocations feature.

这篇关于HttpClient.execute抛出的OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆