首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带改造和rxjava OutOfMemory的分页

带改造和rxjava OutOfMemory的分页
EN

Stack Overflow用户
提问于 2018-06-03 07:13:06
回答 2查看 811关注 0票数 1

我正在尝试手动实现分页。我做的和样本储存库完全一样。但是我有一些无法理解的错误(在下面的日志中)。我的应用程序只能加载2-3页和崩溃。

日志:

代码语言:javascript
复制
06-03 09:55:30.457 22190-22423/example.me E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: example.me, PID: 22190
io.reactivex.exceptions.UndeliverableException: java.lang.OutOfMemoryError: Failed to allocate a 168210 byte allocation with 9792 free bytes and 9KB until OOM
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:64)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 168210 byte allocation with 9792 free bytes and 9KB until OOM
    at java.lang.StringFactory.newStringFromBytes(StringFactory.java:79)
    at java.lang.StringFactory.newStringFromBytes(StringFactory.java:207)
    at okio.Buffer.readString(Buffer.java:631)
    at okio.Buffer.readString(Buffer.java:614)
    at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:254)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
    at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:41)
    at io.reactivex.Observable.subscribe(Observable.java:10838)
    at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
    at io.reactivex.Observable.subscribe(Observable.java:10838)
    at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

或有时:

代码语言:javascript
复制
OutOfMemoryError thrown while trying to throw OutOfMemoryError

网络请求代码:

代码语言:javascript
复制
EventApiService eventApiService = RetrofitClient.getApiService();
    compositeDisposable.add(eventApiService.getJson(citySlug, FIRST_PAGE_TO_LOAD)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::handleFirstEventsDataResponse, this::handleFirstEventsDataResponseError));

响应处理代码(首页):

代码语言:javascript
复制
@Override
public void showData(ResponseData responseData) {
    this.events = responseData.getEvents();
    this.responseData = responseData;
    eventAdapter = new EventAdapter(responseData.getEvents(), eventClickListener);
    recyclerView.setAdapter(eventAdapter);
}

响应处理代码(下一页):

代码语言:javascript
复制
@Override
public void showAdditionalData(ResponseData responseData) {
    eventAdapter.removeLoadingItem();
    isLoading = false;
    eventAdapter.addData(responseData.getEvents());
    eventAdapter.addLoadingItem();
}

改造建筑方法:

代码语言:javascript
复制
private static Retrofit getRetrofitInstance() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(logging);
    OkHttpClient client = builder.build();
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

我想做的事:

  • android:largeHeap="true" in AndroidManifest.它让我们多加载一个页面。
  • 将RxJava请求替换为普通Retrofit2 --没有强烈反应。
  • 去除OkHttpClient从改造建设者-没有强烈的反应。

UPD:

我们发现原因是图像加载。我添加了fit().centerCrop(),现在它加载5-7页并崩溃。我的映像在适配器中加载代码片段:

代码语言:javascript
复制
String imageUrl = "";
            try {
                imageUrl = event.getImages().get(0).getImageUrl();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

            if (eventViewHolder.ivPhoto != null) {
                Picasso.get().load(imageUrl).fit().centerCrop().into(eventViewHolder.ivPhoto);
            }

新崩溃日志:

代码语言:javascript
复制
06-03 10:32:07.791 17978-17986/example.me E/System: Uncaught exception thrown by finalizer
06-03 10:32:08.438 17978-17986/example.me E/System: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
06-03 10:32:08.439 17978-18715/example.me E/AndroidRuntime: FATAL EXCEPTION: Thread-163055
Process: example.me, PID: 17978
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
EN

回答 2

Stack Overflow用户

发布于 2018-08-07 02:35:42

当您使用HttpLoggingInterceptor.Level.BODY时,您将尝试下载大文件,将内存中的所有主体保存为日志。

这对OOM来说很容易。

尝试删除日志正文或只记录“无”、“基本”或“标题”,然后再试一次。

当您尝试获取大文件时,可以像这样在retrofit2中使用@Streaming。

代码语言:javascript
复制
@Streaming
@GET
fun downloadFileWithDynamicUrlSync(@Url fileUrl: String): Call<ResponseBody>

试着移除这里的日志体。

代码语言:javascript
复制
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

试试这个。

代码语言:javascript
复制
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.NONE);
票数 2
EN

Stack Overflow用户

发布于 2018-06-03 08:19:51

也许,当毕加索尝试设置图像时,viewholder已经为空。为了进行调试,您可以使用picasso独立加载映像并自行设置映像。不要忘记在设置之前检查空值。

代码语言:javascript
复制
Picasso.with(context)
            .load(imageUrl)
            .into(imageView, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {
                            //check nullability and set image

                        }

                        @Override
                        public void onError() {

               }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50664094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档