Hack Forums
[C#] Collect singleton instance before owning application has terminated - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Collect singleton instance before owning application has terminated (/showthread.php?tid=5927232)



Collect singleton instance before owning application has terminated - itcorp - Jan 7, 2019

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.


RE: Collect singleton instance before owning application has terminated - Neos07 - Jan 7, 2019

If you can show some code, I'd have the pleasure to help.


RE: Collect singleton instance before owning application has terminated - itcorp - Jan 7, 2019

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



RE: Collect singleton instance before owning application has terminated - 0xDEAD10CC - Jan 7, 2019

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..


RE: Collect singleton instance before owning application has terminated - itcorp - Jan 8, 2019

(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.


RE: Collect singleton instance before owning application has terminated - riu41776 - Jan 9, 2019

Exactly what i was about to say, why use singleton in the first place.


RE: Collect singleton instance before owning application has terminated - +Haze - Jan 16, 2019

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/38138100/what-is-the-difference-between-services-addtransient-service-addscoped-and-serv


RE: Collect singleton instance before owning application has terminated - itcorp - Jan 16, 2019

(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/38138100/what-is-the-difference-between-services-addtransient-service-addscoped-and-serv

thanks for your reply