FlycoBanner_Master icon indicating copy to clipboard operation
FlycoBanner_Master copied to clipboard

有内存泄漏问题

Open PengHaiJin opened this issue 8 years ago • 14 comments

有内存泄漏问题

PengHaiJin avatar Jun 04 '16 03:06 PengHaiJin

麻烦把问题说清楚!我也在用,描述出来才能修改!

ysmintor avatar Jun 27 '16 05:06 ysmintor

确实有内存泄露,轮播的线程池应该持有当前界面的context

time007 avatar Jun 29 '16 15:06 time007

你们是如何解决的?我也碰到了内存泄漏问题

lucheng0522 avatar Jul 05 '16 03:07 lucheng0522

我试了试在onPause里调用pauseScroll就没有内存泄露了,onResume里start。轮播的线程占用了context,activity退出时主动停止就行。

time007 avatar Jul 05 '16 05:07 time007

非常感谢你的回答,谢谢你。

lucheng0522 avatar Jul 05 '16 06:07 lucheng0522

在onResume中应该使用goOnScroll()要不然报错

lucheng0522 avatar Jul 05 '16 07:07 lucheng0522

哦,好的,我这暂时木有发现问题呢,哈哈,多谢你的提醒。

time007 avatar Jul 05 '16 07:07 time007

多谢大神们分析解决!

HappyJess avatar Jul 21 '16 02:07 HappyJess

这样子可以解决内存泄露,在onDetachedFromWindow()中结束线程

··· public class SimpleImageBanner extends BaseIndicaorBanner<Course, SimpleImageBanner> {

public SimpleImageBanner(Context context) {
    this(context, null, 0);
}

public SimpleImageBanner(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public SimpleImageBanner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onTitleSlect(TextView tv, int position) {
}

@Override
public View onCreateItemView(int position) {
    Course data = list.get(position);
    ImageView imageView = new ImageView(context);
    int itemWidth = dm.widthPixels;
    int itemHeight = (int) (itemWidth * 430 * 1.0f / 1080);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

// imageView.setLayoutParams(new LinearLayoutCompat.LayoutParams(itemWidth,itemHeight)); imageView.setLayoutParams(new LinearLayout.LayoutParams(itemWidth,itemHeight)); Picasso.with(context).load(data.getImg_src()).into(imageView); return imageView; }

@Override
protected void onDetachedFromWindow() {
    pauseScroll();
    super.onDetachedFromWindow();
}

···

happyHou avatar Aug 02 '16 02:08 happyHou

@happyHou

       在onDetachedFromWindo()里应该加一个是正在滑动的判断再pauseScroll()可能会好些吧?

ysmintor avatar Aug 03 '16 00:08 ysmintor

但是你停止了,还需要再次启动。。 @Override public void onResume() { super.onResume(); convenientBanner.goOnScroll(); }

lucheng0522 avatar Aug 03 '16 01:08 lucheng0522

@ysmintor 没有必要。就是一个轮询线程。看看源码

happyHou avatar Aug 03 '16 02:08 happyHou

@lucheng0522 对。普通的view你在activity或者fragmet中可以拿到它的引用,在合适的生命周期,开始滚动和停止滚动,是不会出现内存泄露的。但是有时候这个view必须在adapter中直接new出来,就不能通过activity的生命周期控制,只能用View的onDetachedFromWindow()的时候shutdwn这个线程。在我自己的项目中遇到这个问题了,经过实际测试,可以解决内存泄露。

happyHou avatar Aug 03 '16 02:08 happyHou

我把作者的线程来定时轮播,改成直接使用Handler来处理了,参见:BaseBanner

feer921 avatar Apr 10 '17 02:04 feer921