Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Android RXJava2 memory performance

I created an Observable(RxJava2 + Volley) that repeat for each 5 seconds,
It works but when I Dump Java Heap(memory),there are many Instance of my Model JAVA class,and it will increase for each time that the Observable get repeating.
Why RX create several instance of my model? How can I use only ONE instance of it?

Model

public RequestFuture<String> getLiveRefreshFuture() {
        RequestFuture<String> future = RequestFuture.newFuture();
        VolleyStringRequest request = new VolleyStringRequest(Request.Method.POST
                , REFRESH_URL
                , future
                , future) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return getRefreshParams();
            }
        };

        VolleySingleton.getInstance().addToRequestQueue(request);
        return future;
    }    

Activity

    private final CompositeDisposable disposables = new CompositeDisposable();

final LiveRemoteModel model = DaggerLiveComponent.builder().build().getModel();

        Observable<LiveResponse> observable = Observable
                .interval(Constants.TOOLBAR_BADGES_REFRESH_DELAY, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .map(dummy -> model.getLiveRefreshFuture())
                .map(RequestFuture::get)
                .map(LiveResponse::new)
                .observeOn(AndroidSchedulers.mainThread());

        DisposableObserver<LiveResponse> disposableObserver =
                new DisposableObserver<LiveResponse>() {
                    @Override
                    public void onNext(@NonNull LiveResponse liveResponse) {
                        setToolbarBadges(liveResponse.getToolbarBadges());
                    }

                    public void onError(@NonNull Throwable e) {
                        Log.e("RX", "onError: ", e);
                    }

                    @Override
                    public void onComplete() {
                        Log.d("RX", "onComplete: ");
                    }
                };

        disposables.add(observable.subscribeWith(disposableObserver));    

Memory Dump

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why RX create several instance of my model? How can I use only ONE instance of it?

If you look carefully the object in the heapdump is LiveRemoteModel$2 which indicates it is an anonymous class within LiveRemoteModel.

Looking at your code this is probably the VolleyStringRequest object that gets created each time model.getLiveRefreshFuture() is called. There is nothing retaining that object within the RX pipeline so there must be something within Volley retaining it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...