Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:53:01+00:00 2026-05-28T05:53:01+00:00

I am using a Singleton instance created out of a nested class. This instance

  • 0

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.

I would like to know 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.

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;
    }    
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:53:01+00:00Added an answer on May 28, 2026 at 5:53 am

    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.

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

    Otherwise

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton class which I would like to be created using Spring's
By using a singleton, only one instance of it can be created. Do we
I am using the singleton pattern in all my PHP class files. Would it
I'm using a singleton class to save all my settings info. It's first utilized
How would one create a Singleton class using PHP5 classes?
This question is about using getter methods of a singleton object in worker threads.
I have created a C++/CLI mixed DLL which I am using from C# Winforms
I'm having issues with a singleton I've created. It contains two NSMutableDictionary's, which are
Originally, I was using my DataContext object as a global singleton. When using this
I've created a singleton to perform some server requests. However for some requests I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.