{"action":"created","issue":{"url":"https://api.github.com/repos/square/okhttp/issues/2482","repository_url":"https://api.github.com/repos/square/okhttp","labels_url":"https://api.github.com/repos/square/okhttp/issues/2482/labels{/name}","comments_url":"https://api.github.com/repos/square/okhttp/issues/2482/comments","events_url":"https://api.github.com/repos/square/okhttp/issues/2482/events","html_url":"https://github.com/square/okhttp/issues/2482","id":148086811,"number":2482,"title":"Retrofit + OkHTTP causes memory leaks in Tomcat","user":{"login":"CatPlanet","id":11427058,"avatar_url":"https://avatars.githubusercontent.com/u/11427058?v=3","gravatar_id":"","url":"https://api.github.com/users/CatPlanet","html_url":"https://github.com/CatPlanet","followers_url":"https://api.github.com/users/CatPlanet/followers","following_url":"https://api.github.com/users/CatPlanet/following{/other_user}","gists_url":"https://api.github.com/users/CatPlanet/gists{/gist_id}","starred_url":"https://api.github.com/users/CatPlanet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CatPlanet/subscriptions","organizations_url":"https://api.github.com/users/CatPlanet/orgs","repos_url":"https://api.github.com/users/CatPlanet/repos","events_url":"https://api.github.com/users/CatPlanet/events{/privacy}","received_events_url":"https://api.github.com/users/CatPlanet/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-04-13T14:54:01Z","updated_at":"2016-04-13T15:32:08Z","closed_at":null,"body":"creation of OkHttpClient with ConnectionPool:\r\n\r\n\t@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)\r\n\t@Bean(name = \"closeableOKHTTP\")\r\n\tpublic OkHttpClient closeableOKHTTP() {\r\n\t\tConnectionPool pool = new ConnectionPool(5, 1, TimeUnit.SECONDS);\r\n\t\tOkHttpClient client = new OkHttpClient.Builder() //\r\n\t\t\t\t.connectTimeout(3, TimeUnit.MINUTES) //\r\n\t\t\t\t.followRedirects(true) //\r\n\t\t\t\t.readTimeout(3, TimeUnit.MINUTES) //\r\n\t\t\t\t.retryOnConnectionFailure(false) //\r\n\t\t\t\t.writeTimeout(3, TimeUnit.MINUTES). //\r\n\t\t\t\tconnectionPool(pool) //\r\n\t\t\t\t.build();\r\n\t\treturn client;\r\n\t}\r\n\r\nbuilding service:\r\n\r\n\tSocialRemoteService getRemote() {\r\n\t\tGson gson = new GsonBuilder() //\r\n\t\t\t\t.setLenient() //\r\n\t\t\t\t.create();\r\n\t\treturn new Retrofit.Builder() //\r\n\t\t\t\t.baseUrl(API) //\r\n\t\t\t\t.client(okHttpClient) //\r\n\t\t\t\t.addConverterFactory(GsonConverterFactory.create(gson)) //\r\n\t\t\t\t.callbackExecutor(daemonExecutor) //\r\n\t\t\t\t.build() //\r\n\t\t\t\t.create(SocialRemoteService.class);\r\n\t}\r\n\r\n\t@PreDestroy\r\n\tpublic void destroy() {\r\n\t\tokHttpClient.connectionPool().evictAll();\r\n\t}\r\n\r\nservice:\r\n\r\n\tpublic interface SocialRemoteService {\r\n\t\r\n\t\t@GET(\"cities\")\r\n\t\tpublic Call<List<SocialCity>> cities(@Query(\"searchField\") String searchField);\r\n\t\r\n\t}\r\n\r\nTomcat logs on un-deploy: http://pastebin.com/3s091BKh\r\n\r\nIs 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.\r\n\r\ndependencies:\r\n\r\n- okhttp3 - 3.2.0\r\n- retrofit2 - 2.0.1\r\n- retrofit2 converter-gson - 2.0.1\r\n- tomcat8 - Apache Tomcat/8.0.32\r\n- java8 - 1.8.0_74-b02"},"comment":{"url":"https://api.github.com/repos/square/okhttp/issues/comments/209512357","html_url":"https://github.com/square/okhttp/issues/2482#issuecomment-209512357","issue_url":"https://api.github.com/repos/square/okhttp/issues/2482","id":209512357,"user":{"login":"JakeWharton","id":66577,"avatar_url":"https://avatars.githubusercontent.com/u/66577?v=3","gravatar_id":"","url":"https://api.github.com/users/JakeWharton","html_url":"https://github.com/JakeWharton","followers_url":"https://api.github.com/users/JakeWharton/followers","following_url":"https://api.github.com/users/JakeWharton/following{/other_user}","gists_url":"https://api.github.com/users/JakeWharton/gists{/gist_id}","starred_url":"https://api.github.com/users/JakeWharton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JakeWharton/subscriptions","organizations_url":"https://api.github.com/users/JakeWharton/orgs","repos_url":"https://api.github.com/users/JakeWharton/repos","events_url":"https://api.github.com/users/JakeWharton/events{/privacy}","received_events_url":"https://api.github.com/users/JakeWharton/received_events","type":"User","site_admin":false},"created_at":"2016-04-13T15:32:07Z","updated_at":"2016-04-13T15:32:07Z","body":"Okio is filed here: https://github.com/square/okio/issues/107\n\nNot sure where the other two come from.\n\nOn Wed, Apr 13, 2016 at 11:25 AM Cat Planet <notifications@github.com>\nwrote:\n\n> changed destroy method to:\n>\n>         okHttpClient.connectionPool().evictAll();\n>         ExecutorService executorService = okHttpClient.dispatcher().executorService();\n>         executorService.shutdown();\n>         try {\n>             executorService.awaitTermination(3, TimeUnit.MINUTES);\n>             log.info(\"OKHTTP ExecutorService closed.\");\n>         } catch (InterruptedException e) {\n>             log.warn(\"InterruptedException on destroy()\", e);\n>         }\n>\n> tomcat logs: http://pastebin.com/9Vm6rtpH\n>\n> —\n> You are receiving this because you commented.\n>\n>\n> Reply to this email directly or view it on GitHub\n> <https://github.com/square/okhttp/issues/2482#issuecomment-209508059>\n>\n"}}