如何解决Android改造中的内存泄漏问题?

时间:2019-01-02 00:31:17

标签: android memory-leaks retrofit2 okhttp

我在一个现有的Android应用程序中遇到了这个问题

retrofit

我在现有的android应用中使用了OkHttp// ok http implementation 'com.squareup.okhttp3:okhttp:3.4.1' //for body in retrofit implementation 'com.squareup.retrofit2:converter-scalars:2.3.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.3.1' // for retro fit implementation 'com.squareup.retrofit2:retrofit:2.3.0' // if you are using proguard follow this link //http://square.github.io/retrofit/ // for rxjava implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 的那些版本,我在logcat中发现很多内存泄漏,因为它说“您是否忘了关闭OkHttp,因为它已经关闭了内存泄漏。.

try{}finally{ response.body().close();}

我发现有些posts建议okhttp的升级版和改造将自动处理内存泄漏,而有些posts建议我需要使用<T>,但由于我有回应属于通用类型.close(),因此我将完全无法访问body().rawResponse().close()方法。由于我可以用来访问 public void refreshRemoteAcademicCalendar(int skip, int top, String type) { Call<AcademicCalendarModel.AcademicCalendarMetaData> request = academicCalendarService.getEventsAcademicCalendar(skip, top, "Type eq '" + type + "'"); academicCalendarService.getEventsAcademicCalendar(skip, top, "Type eq '" + type + "'").enqueue(new Callback<AcademicCalendarModel.AcademicCalendarMetaData>() { @Override public void onResponse(Call<AcademicCalendarModel.AcademicCalendarMetaData> call, Response<AcademicCalendarModel.AcademicCalendarMetaData> response) { try { if (response.isSuccessful()) { List<AcademicCalendarModel> results = response.body().getValue(); for (AcademicCalendarModel academicCalendar : results) { saveDataToDatabase(academicCalendar); } } }finally { response.raw().body().close(); } } @Override public void onFailure(Call<AcademicCalendarModel.AcademicCalendarMetaData> call, Throwable t) { } }); }

java.lang.IllegalStateException: Cannot read the raw response body of a converted body.
                      at retrofit2.OkHttpCall$NoContentResponseBody.source(OkHttpCall.java:267)
                      at okhttp3.ResponseBody.close(ResponseBody.java:187)

这会导致另一个问题:

composer create-project symfony-cmf/sandbox cmf-sandbox

0 个答案:

没有答案