什么是使用在WeakReferences好处? [英] What are the benefits to using WeakReferences?

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

问题描述

我在我的应用程序的一些内存泄漏。他们都来源于身边,我已经花了很多时间调整,尽量减少一个多上下文传递尽可能一个loooot特定视图集群。这使我相信,在集群中使用位图的问题。所以,我想用在WeakReferences为所使用的意见位图的所有引用。我从来没有使用一个WeakReference的,我不知道这是一个很好的应用。任何机构可以提供有帮助的指针或建议吗?

解决方案
  

所以,我想用   在WeakReferences向所有引用   使用的意见位图。我有   从未使用过的WeakReference,我不   知道这是一个很好的应用。   任何机构可以提供有帮助的   指针或建议吗?

请注意,这是很危险的,你的情况。在GC能摆脱所有的位图,而您的应用程序可能仍然需要它们。

有关WeakReference的关键问题是要了解与硬引用的区别。如果没有更多的辛苦参考你的应用程序中的位图,然后GC被允许原子从内存中删除对象和所有现有的弱引用将立即指向空。在你的情况,你不能使用弱引用在你的code。

下面是解决方案的想法。创建一个容器对象,将保持弱引用(只),以所有的位图。您的意见应始终引用位图,只有硬引用。当一个视图中创建一个位图,它应在容器对象存储。当它要使用的图,应该获得从容器的硬参考

这样的,如果没有意见所指的位图,则GC将收集无副作用的观点的对象,因为没有一个是硬引用。当使用弱引用的对象,这是很好的做法,明确设置硬引用为null,当你不需要的对象了。

添加

下面是一个快速实施解决方案(只是给一个想法):

 公共类BitmapContainer {

    公共静态类的位图{
        私人最终长的ID;
        公共位图(长的id){this.id = ID; }
        众长的getId(){返回ID; }
        公共无效平局(){};
    }

    WeakHashMap中<位图,WeakReference的<位图>> myBitmaps
        =新WeakHashMap中<位图,WeakReference的<位图>>();

    公共无效registerBitMap(位图BM){

        如果(BM == NULL)抛出新的NullPointerException();

        WeakReference的<位图> WR =新的WeakReference<位图>(BM);
        myBitmaps.put(BM,WR);

    }

    / **方法返回null,如果位图不可用* /
    公共位图getBitMap(长ID){

        对(位图文件:myBitmaps.keySet()){
            如果(项目!= NULL){
                如果(item.getId()== id)的{
                    归还物品;
                }
            }
        }

        返回null;

    }

}
 

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?

解决方案

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?

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

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.

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.

Addition

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;

    }

}

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

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