Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Collect singleton instance before owning application has terminated

What I want is to gc collect singleton instance after every use,but since singletron istance always present and it's never two or zero garbage collector will never collect it.
If someone has experience with this please for help.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
If you can show some code, I'd have the pleasure to help.
(Jan 7, 2019 06:34 PM)Neos07 Wrote:  If you can show some code, I'd have the pleasure to help.

I made an example of a class with my attempt to collect but in debug i can not see the changes in heap,in order to not post on the forum too much unimportant code.
Code
public class test1
   {
       public string testtest;

   }

   public class TestClass : IDisposable
   {

       private List<string> _collection;
       private test1 _test1;
       private test1 _test2;

       private bool _disposed = false;        

       public string ReturnString()
       {
           return "some string";
       }

       #region Implementacion of IDisposable

       public void Dispose()
       {
           if (!_disposed)
           {
               _disposed = true;
               Dispose(true);
               GC.SuppressFinalize(this);
           }

       }

       protected virtual void Dispose(bool disposing)
       {
           if (disposing)
           {

               instance = null;
           }
       }

       #endregion

       #region Singleton pattern

       private static TestClass instance;

       private TestClass()
       {
           _collection = new List<string>();
           _test1 = new test1();
           _test2 = new test1();
       }

       public static TestClass Shared()
       {
           if (instance == null)
               instance = new TestClass();
           return instance;
       }

       #endregion

       ~TestClass()
       {
           Debug.WriteLine("FINALIZE");
           if (!_disposed)
           {
               _disposed = true;
               Dispose(false);
           }

       }

   }

Solution for now, not what I wanted  but it will help until I find a better solution.i make class as facade to get the opportunity to do cleaning on the parts where I need it.
Code
public class TestFacade
   {
       public string ReturnString()
       {
           using (var sing = TestClass.Shared())
           {
               return sing.ReturnString();
           }
       }

       public static TestFacade Instance { get; } = new TestFacade();
   }
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
That somewhat full dispose pattern is somewhat redundant here but if you need the GC to free this at a certain point why are you using a singleton in the first place? Definitely seems like an anti-pattern case here..
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Jan 7, 2019 10:57 PM)0xDEAD10CC Wrote:  That somewhat full dispose pattern is somewhat redundant here but if you need the GC to free this at a certain point why are you using a singleton in the first place? Definitely seems like an anti-pattern case here..
You are absolutely right, this is anti-pattern but I need a quick solution and i want my preformances to be solid becouse they have no impact on thread-safety or performance,to avoid the reconstruction of my service.But it looks like I'll have to do reconstruction or to try use another patern over which i will have control.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Exactly what i was about to say, why use singleton in the first place.
If you're using a dependency injection framework, then add it as Scoped. There are three lifetimes for dependencies in .NET and for your case, Singleton is the opposite of what you need.
[Image: Ysc7I8m.png]

https://stackoverflow.com/questions/3813...d-and-serv
(Jan 16, 2019 03:30 PM)+Haze Wrote:  If you're using a dependency injection framework, then add it as Scoped. There are three lifetimes for dependencies in .NET and for your case, Singleton is the opposite of what you need.
[Image: Ysc7I8m.png]

https://stackoverflow.com/questions/3813...d-and-serv

thanks for your reply
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by KαѕρerMay 1, 2020 12:22 PM 3 157 May 1, 2020 12:22 PM
Last Post: Shadow_Cloud
[Python] by Meelo XOMay 11, 2019 05:52 AM 0 119 May 11, 2019 05:52 AM
Last Post: Meelo XO
[Java] by TennoFeb 3, 2019 04:49 AM 4 307 Feb 3, 2019 04:49 AM
Last Post: Tenno