使用 Wea​​kReferences 有什么好处? [英] What are the benefits to using WeakReferences?

查看:20
本文介绍了使用 Wea​​kReferences 有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些内存泄漏.它们都起源于一个特定的视图集群,我花了很多时间调整并试图尽可能减少上下文传递.这让我相信集群中使用的位图是问题所在.所以我想对视图使用的位图的所有引用使用 Wea​​kReferences.我从未使用过 WeakReference 并且不确定这是否是一个好的应用程序.任何机构都可以提供有用的指示或提示吗?

I have some memory leaks in my app. They all originate around a specific view cluster that I have spent a loooot of time tweaking and trying to reduce a much contextual passing as possible. This leads me to believe that bitmaps used in the cluster are the issue. So I was thinking to use WeakReferences for all references to the bitmaps used by the views. I have never used a WeakReference and am not sure if this is a good application. Can any body provide an helpful pointers or tips?

推荐答案

所以我想用WeakReferences 对所有引用视图使用的位图.我有从未使用过 WeakReference 并且我没有确定这是否是一个好的应用程序.任何机构都可以提供帮助提示或提示?

So I was thinking to use WeakReferences for all references to the bitmaps used by the views. I have never used a WeakReference and am not sure if this is a good application. Can any body provide an helpful pointers or tips?

小心,这对你来说很危险.GC 可以删除所有位图,而您的应用程序可能仍然需要它们.

Be careful, this is dangerous in your case. The GC could get rid of all your bitmaps while your application may still need them.

WeakReference 的关键问题是理解与硬引用的区别.如果您的应用程序中不再有对位图的硬引用,则允许 GC 从内存中原子地删除该对象,并且所有现有的弱引用将立即指向 null.就您而言,您不能在整个代码中使用弱引用.

The key issue about WeakReference is to understand the difference with hard references. If there is no more hard reference to a bitmap in your application, then the GC is allowed to atomically remove the object from memory and all existing weak reference will instantaneously point to null. In your case, you CANNOT use weak references all over your code.

这是解决方案的想法.创建一个容器对象,该对象将保持对所有位图的弱引用(仅).您的视图应始终仅使用硬引用来引用位图.当一个视图创建一个位图时,它应该在容器对象中注册它.当它想使用一个视图时,它应该从容器中获得一个硬引用.

Here is an idea of the solution. Create a container object that will keep weak references (only) to all your bitmaps. Your views should always reference bitmaps with hard references only. When a view creates a bitmap, it should register it in the container object. When it wants to use a view, it should obtain a hard reference from the container.

像这样,如果没有视图引用位图,那么 GC 将收集对象而不会对视图产生副作用,因为没有人对其进行硬引用.使用弱引用对象时,当您不再需要该对象时,最好将硬引用显式设置为 null.

Like that, if no views is referring to a bitmap, then the GC will collect the object without side effects for views, since none has a hard reference to it. When using weakly referenced objects, it is good practice to explicitly set hard references to null when you don't need the object anymore.

添加

这是解决方案的快速实现(只是为了提供一个想法):

Here is a quick implementation of the solution (just to give an idea):

public class BitmapContainer {

    public static class Bitmap {
        private final long id;
        public Bitmap(long id) { this.id = id; }
        public long getId() { return id; }
        public void draw() { };
    }

    WeakHashMap<Bitmap, WeakReference<Bitmap>> myBitmaps
        = new WeakHashMap<Bitmap, WeakReference<Bitmap>>();

    public void registerBitMap(Bitmap bm) {

        if ( bm == null ) throw new NullPointerException();

        WeakReference<Bitmap> wr = new WeakReference<Bitmap>(bm);
        myBitmaps.put(bm, wr);

    }

    /** Method returns null if bitmap not available */
    public Bitmap getBitMap(long id) {

        for ( Bitmap item : myBitmaps.keySet() ) {
            if ( item != null) {
                if ( item.getId() == id ) {
                    return item;
                }
            }
        }

        return null;

    }

}

这篇关于使用 Wea​​kReferences 有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆