- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我刚开始使用 RxAndroid 和 RxJava。我正在使用 RxJava + Retrofit2 发出 GET 请求。我正在使用以下代码执行大约 1500 个 GET 请求并出现内存不足错误。然而,这次相同的代码只是经过改造,没有 RxAndroid 并且它可以工作。所以我的结论是我在 RxAndroid 中做错了什么。你能帮我解决我遗漏的问题吗?
代码示例:
Subject<Story> mStoryEmitter = PublishSubject.create();
private void getStory(int storyID) {
HNApi.Factory.getInstance().getStory(storyID).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getStoryObserver());
}
mStoryListEmitter.subscribe(new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {}
@Override
public void onNext(List<Integer> value) {
if(mRecyclerView != null) {
mRecyclerView.setAdapter(null);
if(mAdapter != null) {
mAdapter.clear();
mAdapter = null;
}
}
mAdapter = new SimpleRecyclerViewAdapter();
mRecyclerView.setAdapter(mAdapter);
for(Integer storyID : value) {
getStory(storyID);
}
}
@Override
public void onError(Throwable e) {}
@Override
public void onComplete() {}
});
private DisposableObserver<Story> getStoryObserver() {
DisposableObserver<Story> observer = new DisposableObserver<Story>() {
@Override
public void onNext(Story value) {
mStoryEmitter.onNext(value);
dispose();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
return observer;
}
错误:
Throwing OutOfMemoryError "Could not allocate JNI Env"
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.OutOfMemoryError: Could not allocate JNI Env
at java.lang.Thread.nativeCreate(Native Method)
at java.lang.Thread.start(Thread.java:1063)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1556)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:310)
at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:543)
at java.util.concurrent.ScheduledThreadPoolExecutor.submit(ScheduledThreadPoolExecutor.java:642)
at io.reactivex.internal.schedulers.NewThreadWorker.scheduleActual(NewThreadWorker.java:120)
at io.reactivex.internal.schedulers.IoScheduler$EventLoopWorker.schedule(IoScheduler.java:221)
at io.reactivex.Scheduler.scheduleDirect(Scheduler.java:130)
at io.reactivex.Scheduler.scheduleDirect(Scheduler.java:109)
AppData::create pipe(2) failed: Too many open files
at io.reactivex.internal.operators.observable.ObservableSubscribeOn.subscribeActual(ObservableSubscribeOn.java:36)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.internal.operators.observable.ObservableObserveOn.subscribeActual(ObservableObserveOn.java:44)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at com.example.MainActivity.getStory(MainActivity.java:100)
at com.example.MainActivity.access$300(MainActivity.java:25)
at com.example.MainActivity$2.onNext(MainActivity.java:67)
at com.example.MainActivity$2.onNext(MainActivity.java:49)
at io.reactivex.subjects.PublishSubject$PublishDisposable.onNext(PublishSubject.java:263)
at io.reactivex.subjects.PublishSubject.onNext(PublishSubject.java:182)
at com.example.MainActivity$5.onNext(MainActivity.java:147)
at com.example.MainActivity$5.onNext(MainActivity.java:138)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:198)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:250)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
... 7 more
AppData::create pipe(2) failed: Too many open files
FATAL EXCEPTION: main
Process: com.example, PID: 15857
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.OutOfMemoryError: Could not allocate JNI Env
at java.lang.Thread.nativeCreate(Native Method)
at java.lang.Thread.start(Thread.java:1063)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1556)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:310)
at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:543)
at java.util.concurrent.ScheduledThreadPoolExecutor.submit(ScheduledThreadPoolExecutor.java:642)
at io.reactivex.internal.schedulers.NewThreadWorker.scheduleActual(NewThreadWorker.java:120)
at io.reactivex.internal.schedulers.IoScheduler$EventLoopWorker.schedule(IoScheduler.java:221)
at io.reactivex.Scheduler.scheduleDirect(Scheduler.java:130)
at io.reactivex.Scheduler.scheduleDirect(Scheduler.java:109)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn.subscribeActual(ObservableSubscribeOn.java:36)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.internal.operators.observable.ObservableObserveOn.subscribeActual(ObservableObserveOn.java:44)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at com.example.MainActivity.getStory(MainActivity.java:100)
at com.example.MainActivity.access$300(MainActivity.java:25)
at com.example.MainActivity$2.onNext(MainActivity.java:67)
at com.example.MainActivity$2.onNext(MainActivity.java:49)
at io.reactivex.subjects.PublishSubject$PublishDisposable.onNext(PublishSubject.java:263)
at io.reactivex.subjects.PublishSubject.onNext(PublishSubject.java:182)
at com.example.MainActivity$5.onNext(MainActivity.java:147)
at com.example.MainActivity$5.onNext(MainActivity.java:138)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:198)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:250)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
... 7 more
最佳答案
在 RxAndroid 上发布了相同的问题github.
JakeWharton 的回复很明显
The problem is that Schedulers.io() uses a cached thread pool without a limit and thus is trying to create 1500 threads. You should consider using a Scheduler that has a fixed limit of threads, or using RxJava 2.x's parallel() operator to parallelize the operation to a fixed number of workers.
If you're using raw Retrofit by default it uses OkHttp's dispatcher which limits the threads to something like 64 (with a max of 5 per host). That's why you aren't seeing it fail.
If you use createAsync() when creating the RxJava2CallAdapterFactory it will create fully-async Observable instances that don't require a subscribeOn and which use OkHttp's Dispatcher just like Retrofit would otherwise. Then you only need observeOn to move back to the main thread, and you avoid all additional thread creation.
关于android - 内存不足错误 RxAndroid + RxJava + Retrofit2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559218/
我正在尝试迁移我的应用程序以使用 RxJava。我已经在使用 Retrofit,因此我正在尝试使用方法返回 Observables 的 Retrofit 接口(interface)。但是我现在在针对它
我想 post 数据如下: { "user_id":"14545646", "list":["4545645","4545645","4545645","4545645"]
我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办? @FormUrlEncoded @POST("/
我想异步执行 2 个网络调用 - 我正在使用 Retrofit+RxJava 来完成这个。这个逻辑来自一个简单的 Runner 类来测试解决方案。注意:这主要涉及服务器端的 RxJava。 我的代码如
我最近在改造的存储库中使用 Flow。 Result 的密封类 enum class ApiStatus{ SUCCESS, ERROR, LOADING } sealed c
我目前正在开发 Retrofit2 客户端的错误处理部分(使用 Retrofit 的 Rx 实现)。 找了一段时间,没有找到简单的方法将Retrofit返回的ResponseBody反序列化到我报错的
我从 retrofit 更新到 retrofit2 后出现此错误。 FATAL EXCEPTION: OkHttp Dispatcher Process: nz.co.datacom.mars.jun
在使用 RxJava 和 Retrofit 2 时,我正在尝试创建单元测试来覆盖我的应用何时收到特定响应。 我遇到的问题是,在 Retrofit 2 中,我看不到在不使用反射的情况下创建 retrof
在 Retrofit 1.9.x 中有一个 RetrofitError.Kind.NETWORK这让您可以轻松确定故障是否由网络错误引起。在新的 2.0 API 中,我们不再有 RetrofitErr
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
在项目上安装这个库之后: compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0
在 Retrofit 1.x 中,我使用以下模式创建 API 服务类,该类模拟某些构建变体的不良网络连接。 // Retrofit 1 private T create(Class apiServi
Retrofit请求API格式(Android): @POST("getOrderStatus") @Headers("Content-Type:application/json") Obser
当我实例化 RestAdapter 时,我的应用程序总是崩溃 private void submitForm(SignupForm form){ RestAdapter adapter = n
我正要从 retrofit 1.9 迁移到最新版本并遇到问题... 我的设置: 三星 Galaxy S7 Edge(Android 7) 改造 2.3 OkHttp 3.8 迁移后突然遇到这个问题:
我正在尝试在 2.5.1-SNAPSHOT 中使用 retrofit 的协程支持,但我不断收到一个奇怪的异常。 我的改造服务类有: @GET("weather") suspend fun getFor
我正在尝试 Ktor通过转换一些当前正在使用的现有项目 Retrofit . 虽然我可以很容易地将请求转换为: client.get { url("$BASE_URL/something/so
使用改造 2,如何为上传的文件设置动态名称? 目前是这样的: @Part("avatar\"; filename=\"image\" ") RequestBody image, 但是,上传的文件名将是
我是一名优秀的程序员,十分优秀!