如何以及何时处置/垃圾回收单一实例 [英] How and when to dispose/garbage collect a singleton instance

查看:176
本文介绍了如何以及何时处置/垃圾回收单一实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用创造出来的一个嵌套类的单一实例。这个实例包含当辛格尔顿布置这是清除一些静态的藏品,但问题是我得到非空处置辛格尔顿参考这是不正确的垃圾回收。

I am using a Singleton instance created out of a nested class. This instance holds some static collections which are cleared when the Singleton is disposed, but the problem is I get a reference to non-null disposed Singleton which is not properly garbage collected.

我想知道何时以及如何彻底处理和垃圾收集我的Singleton实例,这样当实例处置后再次查询(并设置为null)一个新的实例被创建。

I would like to know WHEN and HOW to completely dispose and garbage collect my Singleton instance so that when the instance is queried again after dispose (and setting to null) a new Instance is created.

我使用Singleton实例以下嵌套方式:

I am using the following nested pattern for Singleton instance:

public class SingletonClass : IDisposable
{
    private List<string> _collection;

    private SingletonClass()
    {
    }

    public static SingletonClass Instance
    {
        get
        {
            return Nested.Instance; //line 1 - this line returns the non-null instance after dispose and setting the Singleton instance to null which is causing problems
        }
    }

    private void Init()
    {
        _collection = new List<string>();
        //Add data to above collection
    }

    public void Dispose()
    {
        //Release collection
        _collection.Clear();
        _collection = null;
    }

    class Nested
    {
        static Nested()
        {
            Instance = new SingletonClass();
            Instance.Init();
        }

        internal static readonly SingletonClass Instance;
    }    
}



第1行的问题是,SingletonClass的处置后,从客户端类的_collection对象甚至设置= NULL后而SingletonClass实例保持非空变成空。

The problem at line 1 is that after dispose of SingletonClass from client class, the _collection object becomes null while the SingletonClass instance remains non-null even after setting = null.

推荐答案

您'会只需要执行 System.IDisposable的如果您符合以下基本要求:

You'll only need to implement System.IDisposable if you fulfill following basic requirement:

此接口的主要用途是释放非托管资源。

The primary use of this interface is to release unmanaged resources.

然后我会去为类的析构函数调用的Dispose()例如

Then I would go for the destructor of the class and call Dispose() in the example.

否则

垃圾收集器自动释放分配的内存至
当对象不再使用管理的对象。

The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used.

(这不会是一个真正的情况下单身,除非进程结束)

(which won't be the case with a real singleton, unless the process ends)

您可能会更好,如果你使用的是某事像这样

You may be better off, if you are using sth like this

class PseudoSingleton<T>
    where T : new()
{
    private readonly object _lock = new object();
    private T _instance;

    public T Instance
    {
        get
        {
            lock (this._lock)
            {
                if (this._instance != null)
                {
                    this._instance = new T();
                }
                return this._instance;
            }
        }
    }
    public void Reset()
    {
        lock (this._lock)
        {
            this._instance = null;
        }
    }
}

这篇关于如何以及何时处置/垃圾回收单一实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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