0

I am using Retrofit 2 in my Android application, and I have a lot of network calls. I ran into performance issues and after a long research, I saw that I have too many running threads in my app. I printed all the active threads in the app and saw a specific thread that had too many instances (several hundred) by the name of "OkHttp ConnectionPool" - I'm assuming it's connected to my retrofit calls.

Is there a way to prevent this thread from flooding the system?

Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
Avi Barel
  • 21
  • 3

2 Answers2

1

IMHO in this kind of scenario it would be in your best interest to use a dependency injection framework alongside Retrofit. Like you said retrofit connections are quite expensive. I'm not an expert to give you much advice but it would really help if you looked at tutorials like this retrofit2 combined with dagger2. I've worked with retrofit and come across issues as you mention and this is because connection pool was not optimized. Just to give you an overview what dependency injection will do is simply let you keep a single instance of retrofit throughout your application and all you need to do is inject it wherever you need an instance. Dagger2 is what I've noted to be the best in doing the job so far you can check it out at Dagger By Google

You do have another option of using retrofit as a singleton. Once again please read on this as it's a useful design pattern. The singleton will also be easier in terms of implementing, for you but DI is going to help you on the long run in terms of app scope. For singleton take a look at this Stackoverflow.com answer

Once again please correct me if I'm wrong as I'm in the learning process as well :). Cheers

Community
  • 1
  • 1
imaadhrizni
  • 428
  • 4
  • 11
  • how dagger is going to do the work? i am also facing same issues, can you explain? – Raut Darpan Feb 27 '17 at 11:59
  • Firstly I'll suggest to read a bit about dependency injection. What it does is it simplifies the unnecessary creation of retrofit instances in your case. Once you've understood about DI(dependency injection) read that tutorial link I provided above. If you go through that it provides a simple example of using retrofit with dagger – imaadhrizni Feb 27 '17 at 12:01
  • DI is really easy and helpful once you learn the fundamentals. I'd suggest you do quite some research first ;). – imaadhrizni Feb 27 '17 at 12:03
  • Thanks ill look forward for the same – Raut Darpan Feb 27 '17 at 12:05
  • You do have another option which may be simpler that is to use retrofit as a singleton but if you're looking to expand your app in terms of libraries DI would be helpful on the long run – imaadhrizni Feb 27 '17 at 12:07
  • sorry but i am not familiar with singleton... can you explain? – Raut Darpan Feb 27 '17 at 12:18
  • I added another stack overflow answer above check – imaadhrizni Feb 27 '17 at 12:21
  • And why do you think that author creates a lot of instances of Retrofit? – Divers Feb 27 '17 at 12:26
  • Actually @imaadhrizni is right. But here i would add that beginner will take to learn dagger 2. If you are beginner then first go with singleton. Then move to dagger 2. Since singleton and other scopes play major role in Dagger – Zeeshan Shabbir Feb 27 '17 at 12:26
1

Can you post your code? Are you creating multiple instances of OkHttpClient? Each instance will bring along a new connection pool, so if you have a bunch of these in your application's lifecycle, it's going to get flooded.

You'll want to instantiate OkHttpClient as a singleton. See this comment in the OkHttp source code for more info.

If you need to modify the OkHttpClient to have a different configuration you can use the newBuilder() method, which will reuse the existing connection pool.

Culp
  • 41
  • 4