OkHttp 内存溢出问题 A connection to xxxxxx was leaked.

文章讲述了在使用OkHttp进行API调用时遇到的内存泄漏问题,由于在循环调用中未正确关闭ResponseBody导致。作者尝试了手动关闭Response,但只对第一个循环有效。问题根源在于调用了`response.body().string()`,该方法不会自动关闭响应。为解决此问题,作者选择不读取返回值,直接返回Response。
摘要由CSDN通过智能技术生成

OkHttp内存溢出

最近刚接触OkHttp,使用它调用其他的接口,我测试的时候单个调用或者少量多个调用也没问题,因为是钉钉提醒,我没办法做大规模测试,所以使用检查几遍逻辑和代码没问题就上线了,上线第一天job跑的时候就报了

2023-05-15 09:14:45.986  WARN 45019 --- [ ConnectionPool] okhttp3.OkHttpClient                  : 
A connection to xxxxxx was leaked. 
Did you forget to close a response body? 
To see where this was allocated, set the OkHttpClient logger level to FINE:Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);

在百度找了很久,解决方式都不对,有的人说是因为reponse未手动关闭,需要手动关闭,但是我的是循环调用,手动关闭后只会执行第一个循环,后面发现是因为在reponse 中,实例化了reponse.body().string,是string的方法没有自动关闭reponse 的功能,由于我不需要接受返回值,于是我直接返回reponse ,解决问题.

我的程序逻辑:

 public Response  sendMessageDingTalk(String  id,String token) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{"xxxxxxxx"
        Request request = new Request.Builder()
                .url(sendUrl)
                .method("POST", body)
                .addHeader("Authorization", "Bearer "+token)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        return  response;
    }
E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: com.example.read, PID: 22568 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:111) at java.lang.Thread.run(Thread.java:920) Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at okhttp3.internal.connection.RealRoutePlanner.planConnectToRoute$okhttp(RealRoutePlanner.kt:195) at okhttp3.internal.connection.RealRoutePlanner.planConnect(RealRoutePlanner.kt:152) at okhttp3.internal.connection.RealRoutePlanner.plan(RealRoutePlanner.kt:67) at okhttp3.internal.connection.FastFallbackExchangeFinder.launchTcpConnect(FastFallbackExchangeFinder.kt:118) at okhttp3.internal.connection.FastFallbackExchangeFinder.find(FastFallbackExchangeFinder.kt:62) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:267) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:158) at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:106) at java.lang.Thread.run(Thread.java:920) 怎么解决
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的程序员1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值