locked
Lifetime of a Singelton RRS feed

  • Question

  • User-1319049494 posted

     Hi,

    I am creating a class library , and i need to use Singelton pattern. Just wondering if the Grabage collector

    will ever clear the object created in Singelton class. How garbage collector deals with singelton?

    Thanks

    Thursday, January 7, 2010 6:22 PM

Answers

  • User-158764254 posted

    The singleton pattern generally uses a static/shared variable to hold a reference to the singleton instance

    http://wiki.asp.net/page.aspx/288/singleton/

    since the shared variable represents an active reference to that singleton, the GC does not collect the singleton instance. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 7, 2010 9:14 PM

All replies

  • User-158764254 posted

    The singleton pattern generally uses a static/shared variable to hold a reference to the singleton instance

    http://wiki.asp.net/page.aspx/288/singleton/

    since the shared variable represents an active reference to that singleton, the GC does not collect the singleton instance. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 7, 2010 9:14 PM
  • User-331473013 posted

    How did you implement your singleton class?  The manner in which the GC handles your object will depend on the context  in which the Singleton class was implemented.  In other words, is the intent that the singleton should be applied over the lifetime of a single page, a session or the whole application?


    Traditionally, the Singleton pattern is implemented using a static variable to store a reference to the current object in memory.  If this is consistent with your implementation, then the garbage collector will only destruct your object once your application closes. (For web applications - when your website is stopped on IIS, the server is shutdown, etc...).


    If you can place the code of your Singleton class here, we should be able to give a better, more specific answer to your question.


    Hope this helps.


    Regards

    Monday, January 11, 2010 3:22 AM
  • User-359444340 posted

    Due to the static nature of the single ton class we have to call GC explicitily, as we know that the single ton class have the static class and it will consume the memory till the instance exists of that class i.e (single ton). So we have to call it explicility.


    Tuesday, January 12, 2010 5:28 AM