简体   繁体   English

片段中的内存泄漏问题

[英]Memory leak issues in Fragment

I have implemented MVP pattern in my app. 我已经在我的应用中实现了MVP模式。 And I'm using WeakReferences to store View's reference in my Presenter. 我正在使用WeakReferences将View的引用存储在Presenter中。 But still my fragments are not being claimed by GC upon destroying. 但是我的碎片仍然没有被GC销毁。 Below is the screenshot of problem. 下面是问题的屏幕截图。 Any idea what is causing this and how to remove this issue? 任何想法是什么原因造成的,以及如何消除此问题?

Android Profiler屏幕截图

Below is the code for my Presenter: 以下是我的演示者的代码:

public class ProductDetailPresenter implements ProductDetailContract.Presenter {

private final WeakReference<ProductDetailContract.View> view;
private CategoriesDataSource repo;

public ProductDetailPresenter(ProductDetailContract.View view, CategoriesDataSource repo) {
    this.view = new WeakReference<>(view);
    this.repo = repo;
    view.setPresenter(this);
}

@Override
public void start() {

}

@Override
public void submitRating(final Product product, final float mRating) {
    final ProductDetailContract.View view =ProductDetailPresenter.this.view.get();

    if (view != null) {
        repo.submitRating(product.getId(), mRating, true, new CategoriesDataSource.SubmitRatingCallback() {

            @Override
            public void onRatingSubmitted() {

                product.setRating(mRating);
                product.setRated(true);
                product.setUpdatedAt(new Date(System.currentTimeMillis()));
                repo.updateProductInDB(product);

                if (!view.isActive()) return;

                view.onRatingSubmitted(true, mRating);
            }

            @Override
            public void onError(Throwable throwable) {
                if (!view.isActive()) return;

                view.onRatingSubmitted(false, 0);
            }
        });
    }
}

@Override
public void onRateKarenClicked() {
    ProductDetailContract.View view = this.view.get();
    if (view != null) {
        view.openDialog();
    }
}

@Override
public void onAbhiKhareediyeClicked(Product product) {

    EventBus.getDefault().post(
            new ProductDetailContract.ContractEventMessages(
                    ProductDetailContract.ContractEventMessages.EVENT_START_QUANTITY_SCREEN, product));

}

} }

This is the problem: 这就是问题:

    @Override
    public void submitRating(final Product product, final float mRating) {
       final ProductDetailContract.View view =ProductDetailPresenter.this.view.get(); <-- this is bad

you have a final object that is being passed to the repo. 您有一个最终对象正在传递到存储库。 Delete the whole line. 删除整行。 You don't need it. 不用了 Use in the view.get() inside the onRatingSubmitted and onError 在onRatingSubmitted和onError内部的view.get()中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM