gpt4 book ai didi

java - 使用 Otto 的内存泄漏 Leakcanary 报告

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:37 24 4
gpt4 key购买 nike

跟进 my last question ,这是我无法摆脱的第二次内存泄漏..

我读到我需要根据 Activity 和 Fragment 生命周期注册和取消注册我的静态 Otto 总线,所以我添加了对 onStop 和 onStart 的注册和取消注册调用...有一个按钮可以触发一个事件,然后接收到通过 Viewpager 中的一些 fragment 给我以下内存泄漏:

D/LeakCanary﹕ In com.doesnthaveadomain.leo.calendartracker:1.0:1.
D/LeakCanary﹕ * com.doesnthaveadomain.leo.calendartracker.MyFragment has leaked:
D/LeakCanary﹕ * GC ROOT static com.doesnthaveadomain.leo.calendartracker.MyBus.BUS
D/LeakCanary﹕ * references com.squareup.otto.Bus.handlersByType
D/LeakCanary﹕ * references java.util.concurrent.ConcurrentHashMap.table
D/LeakCanary﹕ * references array java.util.concurrent.ConcurrentHashMap$Node[].[3]
D/LeakCanary﹕ * references java.util.concurrent.ConcurrentHashMap$Node.val
D/LeakCanary﹕ * references java.util.concurrent.CopyOnWriteArraySet.al
D/LeakCanary﹕ * references java.util.concurrent.CopyOnWriteArrayList.elements
D/LeakCanary﹕ * references array java.lang.Object[].[0]
D/LeakCanary﹕ * references com.squareup.otto.EventHandler.target
D/LeakCanary﹕ * leaks com.doesnthaveadomain.leo.calendartracker.MyFragment instance

我的代码..

主要 Activity

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), this);
mViewPager.setAdapter(myAdapter);
mViewPager.setCurrentItem(5);

Button button = (Button) findViewById(R.id.do_something);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyBus.getInstance().post(new SomeEvent("Something"));
}
});
}

static class SomeEvent {
String mString;
SomeEvent(String string) {
mString = string;
}
public String getString() {
return mString;
}
}

}

class MyAdapter extends FragmentStatePagerAdapter {

private int mNumberOfViews;
private final WeakReference<AppCompatActivity> mActivityWeakRef;

public MyAdapter(FragmentManager fm,
AppCompatActivity activity) {
super(fm);

mActivityWeakRef = new WeakReference<AppCompatActivity>(activity);
mNumberOfViews = 10;
}

@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragment();
AppCompatActivity activity = mActivityWeakRef.get();
if (activity != null) {
MyApp.getRefWatcher(mActivityWeakRef.get()).watch(myFragment);
}
return myFragment;
}

@Override
public int getCount() {
return mNumberOfViews;
}
}

我的 fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.otto.Subscribe;

/**
* A placeholder fragment containing a simple view.
*/
public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}

@Override
public void onStart() {
super.onStart();

MyBus.getInstance().register(this);
}

@Override
public void onStop() {
super.onStop();

MyBus.getInstance().unregister(this);
}

@Subscribe
public void onEvent(MainActivity.SomeEvent event) {

}

}

布局,以防万一.. MainActivity:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/AppTheme.Toolbar"
app:popupTheme="@style/AppTheme.Toolbar.Popup">

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<Button
android:id="@+id/do_something"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DO"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

fragment :

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_vertical_margin"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

</LinearLayout>

最佳答案

您在错误的时间使用观看引用。仅当您绝对确定thing 将被垃圾回收时,才应调用watch(thing)。对于您的 Activity 和 Fragments,您需要这样的东西:

@Override public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}

来自 LeakCanary FAQ "How do I use it?" section

关于java - 使用 Otto 的内存泄漏 Leakcanary 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com