当使用投掷的OutOfMemoryError的HttpClient [英] Throwing OutOfMemoryError when using httpclient

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

问题描述

我想创建HTTP请求和发送数据到我的PHP服务器。对于此我使用Android系统的异步HTTP通过循环J库。事情工作正常,但是当我开始上传数据是从我的本地数据库约1600条记录。我得到的内存泄漏错误:

I am trying to create http request and send data to my php server. For this I am using android-async-http by loopj library. Things are working fine but when I start uploading data that are about 1600 records from my local database. I get memory leak error:

12917-12917/com.ylg.all E/art﹕ Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"
      07-29 21:56:51.852  12917-12917/com.ylg.all E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ylg.all, PID: 12917
java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
        at java.lang.Thread.nativeCreate(Native Method)
        at java.lang.Thread.start(Thread.java:1063)
        at org.apache.http.impl.conn.tsccm.AbstractConnPool.enableConnectionGC(AbstractConnPool.java:145)
        at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.createConnectionPool(ThreadSafeClientConnManager.java:125)
        at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:103)
        at com.loopj.android.http.AsyncHttpClient.<init>(AsyncHttpClient.java:238)
        at com.loopj.android.http.AsyncHttpClient.<init>(AsyncHttpClient.java:177)
        at com.loopj.android.http.AsyncHttpClient.<init>(AsyncHttpClient.java:147)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
        at android.app.ActivityThread.access$2100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

不知道如何解决这个问题呢?

Not sure how to solve this issue?

下面是code我想:

我从本地数据库里面拉环我打电话的方法中的数据。是这样的:

I am pulling the data from local DB inside loop I am calling method. something like this:

List<TextDetails> unTextList = dbvalue.getTextData();
for (TextDetails td : unTextList)
{
String textID = td.getSerialNumber();
String textSMS = td.getText();
String textAddress = td.getFulladdress();
String textDate = td.getFulldate();
String textException = td.getExceptiontext();


textDetailsBackUpDataOnline(textID , textSMS, textAddress, textDate, textException);
}

private void textDetailsBackUpDataOnline(final String textID ,
String textSMS, String textAddress, String textDate, String textException)
{
final String uploadWebsite = url_backup_text_details;

RequestParams requestParams = new RequestParams();

requestParams.put("textSMS", textSMS);
requestParams.put("textAddress", textAddress);
requestParams.put("textDate", textDate);
requestParams.put("textException", textException);

Text_HttpClient.post(uploadWebsite, requestParams, new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
{
Log.e("textID", "= how many times");
}

@Override
public void onFailure(int statusCode, org.apache.http.Header[] headers, byte[] errorResponse, Throwable e)
{
e.printStackTrace(System.out);
}
});
}

Text_HttpClient类有以下内容:

Text_HttpClient class has the following:

public class Text_HttpClient 
{
private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
{
client.get(url, params, responseHandler);
}

public static void post(String url, RequestParams requestParams, AsyncHttpResponseHandler responseHandler)
{
client.post(url, requestParams, responseHandler);
}

}

有人可以指导我如何解决这个问题呢?难道还有比上述更好的方式来上传文本之大?

Can somebody guide me on how to solve this issue? Is there a better way than above to upload text that large?

谢谢!

推荐答案

让我们先看看OS和HTTP如何工作的:

当我们将数据发送到世界我们的操作系统基本上是一个正确的发送者..!

When we send data to the world our OS is basically a sender right..!

所以,当我们发送使用predefined大小,然后发送到世界(服务器)大型数据OS使数据块。

So when we send large data OS makes chunks of data using predefined size and then send to the world(server).

现在,让我们看看如何HTTP这一数据的工作:


  • 据缓存数据,直到数据发送完毕。

  • It buffers the data till the completion of data sending.

整个大数据的发送完成后它去结果
过程数据进一步。

After completion of the sending of whole large data it goes to
process that data further.

因此,虽然我们的操作系统的块发送数据不会在块处理的权利...!

在您的情况:您要发送大量数据到服务器。您的操作系统可以发送它,甚至服务器可以接受,但(可能),HTTP或服务器无法处理它。

In your case : you are sending large data to server. Your OS could send it and even server could receive it but (may be) HTTP or server couldn't handle it.

解决方案是:所以,最好的,如果你的数据是独立的那么块发送数据的服务器可以处理它的方式

Solution is: So best way if your data is independent then send data in chunks that server could handle it.

在你的情况的数据是独立的,因此您可以读取和时间以服务器发送一些记录。您可以使用循环发送整个数据。

In your case data is independent so you can read and send some records at a time to server. You can use loop for sending whole data.

编辑:

宾果......它不是真正的服务器错误!

请参阅你的日志,它告诉它不能创建线程。

See your logs, it is telling that it couldn't create thread.

AsyncTask is a thread internally.

由于较少的内存的Andr​​oid的过程中对内存分配比桌面OSs.See位不同的Andr​​oid的流程管理来看,Android不为一个进程分配过大内存。

Because of less memory Android's memory allocations for processes is bit different than the Desktop OSs.See in android's process management view, Android doesn't allocate too big memory for a process.

您的操作系统无法处理的HTTP请求这么多的数据读取和装载,结果是将数据发送到世界前应用程序崩溃。

Your OS couldn't handle this much data reading and loading for HTTP request, result is application crashed before sending data to the world.

解决方案问题还是一样的,只是我已编辑的原因... :)!

Solution for the problem is still same, just I have edited the cause ... :) !

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

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