PDA

View Full Version : RestTemplate / HttpAccessor seems to be leaking threads on Android



thilo.ginkel
Sep 25th, 2011, 07:23 AM
Hi there,

I am using RestTemplate from spring-android-rest-template 1.0.0.M4, which I construct on demand using

new RestTemplate();
or

new RestTemplate(new SimpleClientHttpRequestFactory());

Unfortunately, both constructors are leaking threads whose lifetime exceeds that of the respective RestTemplate instance as HttpAccessor has a private field that initializes a new HttpComponentsClientHttpRequestFactory anyway:


private ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

The spawned threads (one per RestTemplate instance) look like this:


Daemon Thread [<17> RefQueueWorker@org.apache.http.impl.conn.tsccm.Con nPoolByRoute@44eb3ce8] (Running)

The threads are cleaned up in a finalizer, but given a sufficient amount of RestTemplate creations, the application will still crash due to a depletion of resources.

Any idea?

Thanks,
Thilo

Roy Clarkson
Nov 9th, 2011, 03:14 PM
Thilo, I've added a JIRA issue for this, and will plan a fix in the 1.0.0.RC1. Thanks for the feedback!

https://jira.springsource.org/browse/ANDROID-61

Roy Clarkson
Nov 17th, 2011, 04:57 PM
As I mentioned in the JIRA, we should probably default to SimpleClientHttpRequestFactory in Android 2.2 and newer, so the HttpComponentsClientHttpRequestFactory instance is not created. I'm also looking into switching from using the DefaultHttpClient to AndroidHttpClient which is a customized version of DefaultHttpClient for Android. Unfortunately, it is only available in Android 2.2, and we still need to support 2.1 in Spring Android.