Retrofit + OkHTTP causes memory leaks in Tomcat
See original GitHub issuecreation of OkHttpClient with ConnectionPool:
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean(name = "closeableOKHTTP")
public OkHttpClient closeableOKHTTP() {
ConnectionPool pool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
OkHttpClient client = new OkHttpClient.Builder() //
.connectTimeout(3, TimeUnit.MINUTES) //
.followRedirects(true) //
.readTimeout(3, TimeUnit.MINUTES) //
.retryOnConnectionFailure(false) //
.writeTimeout(3, TimeUnit.MINUTES). //
connectionPool(pool) //
.build();
return client;
}
building service:
SocialRemoteService getRemote() {
Gson gson = new GsonBuilder() //
.setLenient() //
.create();
return new Retrofit.Builder() //
.baseUrl(API) //
.client(okHttpClient) //
.addConverterFactory(GsonConverterFactory.create(gson)) //
.callbackExecutor(daemonExecutor) //
.build() //
.create(SocialRemoteService.class);
}
@PreDestroy
public void destroy() {
okHttpClient.connectionPool().evictAll();
}
service:
public interface SocialRemoteService {
@GET("cities")
public Call<List<SocialCity>> cities(@Query("searchField") String searchField);
}
Tomcat logs on un-deploy: http://pastebin.com/3s091BKh
Is there any way to prevent memory leaks using retrofit and okhttp? okHttpClient.connectionPool().evictAll()
even with new ConnectionPool(5, 1, TimeUnit.SECONDS)
does not seem to work.
dependencies:
- okhttp3 - 3.2.0
- retrofit2 - 2.0.1
- retrofit2 converter-gson - 2.0.1
- tomcat8 - Apache Tomcat/8.0.32
- java8 - 1.8.0_74-b02
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:21 (9 by maintainers)
Top Results From Across the Web
How to solve memory leaks problem in android retrofit?
I have used those versions of retrofit and OkHttp in my existing android app and I found in logcat that there are lots...
Read more >Common Tomcat memory issues - MuleSoft
Cause 1 - Memory Leaks. Memory leaks when relaunching applications, a commonly reported problem, are in fact usually traceable to flaws in the...
Read more >OkHttp is quietly retrying requests. Is your API ready? - Medium
GET requests should be idempotent, so retrying them shouldn't cause any problems. POST requests need to be checked on the server side or...
Read more >OKHTTP Library bug causing memory leak - Kite Connect
This bug means that the library opens up a couple of threads (OkHttp-Http2Connection and Okio-Watchdog) , which don't get shut even after the ......
Read more >Android Using Retrofit and OkHttp to get Endpoints-android ...
Coding example for the question Android Using Retrofit and OkHttp to get ... Leak memory in war servlet with tomcat · Returning a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Restarting JVM as a solution to memory leaks in life-cycle aware application sounds pretty flimsy to me, to be honest. It sounds like I would have to restart my Android every 4-5 apps I started and closed.
Direct access to thread pools would do the trick. It might look dirty but in some cases (like using okhttp in server-side apps) it would definitely pay off, especially when pools don’t exit asynchronously as fast as they should.
Different http protocols or code unloading doesn’t really bother me at this point. It’s beacuse I’m unable to manually close those pools which leads to keeping class loader in JVM bothers me.
Can anyone open this issue until it is verified?