question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrofit + OkHTTP causes memory leaks in Tomcat

See original GitHub issue

creation 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
CatPlanetcommented, Apr 18, 2016

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.

We may try to make this work, but code unloading is not an advertised feature of this library

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.

1reaction
CatPlanetcommented, Apr 18, 2016

Can anyone open this issue until it is verified?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found