Tuesday, July 30, 2013

[android-developers] Re: HttpClient GC_Concurrent freed

According to http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e153, " However, the use of EntityUtils is strongly discouraged unless the response entities originate from a trusted HTTP server and are known to be of limited length." If you want to read entity content more than once, You have to use BufferedHttpEntity to wrap HttpEntity.

On Tuesday, July 30, 2013 4:05:26 AM UTC-4, Kristoffer wrote:
Hello.
Thanks for reply.
I have tryed to look at threads and Heap but i could not find what the problem was.
I will try again if i could find anything.

Den måndagen den 29:e juli 2013 kl. 08:22:12 UTC+2 skrev Kristoffer:
Hello.
Iam having trouble with memory leak it will finaly cause the app to force close.
I have stripped down my code just to find where the problem is and i located it to my httpclient.

When i run this code iam geting many GC_Concurrent freed.
if i add a Thread.sleep(1000); then i will ofcourse get less warnings but my guess is that there is still some problem with the code i run.
Is there a way i could prevent the problem.

Here is the code:

Thread c = new Thread() {
       @Override
public void run() {
                                String xml;
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
        HttpGet httpGet;
        HttpClient httpClient;
        HttpResponse httpResponse;
        HttpEntity httpEntity;
        while (RunThread) {

        try {
        httpClient = HttpClientFactory.getThreadSafeClient();
        httpGet = new HttpGet("http://ipaddress/list?format=xml");
        httpGet.addHeader(new BasicScheme().authenticate(creds, httpGet));
        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity, "UTF-8");
       
       
        } catch (Exception e) {
        e.printStackTrace();
       
       
            }
   };
   c.start();
   }

And here is the class HttpClientFactory

public class HttpClientFactory {

    private static DefaultHttpClient client;

    public synchronized static DefaultHttpClient getThreadSafeClient() {
  
        if (client != null)
            return client;
         
        client = new DefaultHttpClient();
        
        ClientConnectionManager mgr = client.getConnectionManager();
        
        HttpParams params = client.getParams();
        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        client = new DefaultHttpClient(
        new ThreadSafeClientConnManager(params,
            mgr.getSchemeRegistry()), params);
  
        return client;
    } 
}


Have a made some huge mistage in the code that is causing this issue?

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate