none
Static property on unmanaged resource - performance

    Pertanyaan

  • i am using

    Xamarin - (mobile apps in C#),

    SQLLight (local database -DB);

    i am having a the class that implement the DB in a Static Property in the Application root.

    this is the class

    public class DB_Generic : IDisposable

    this is the prop - singleton

            public static DB_Generic DB_Generic
            {
                get
                {
                    if (_DBGeneric == null)
                        _DBGeneric = new DB_Generic(DependencyService.Get<IFileHelper>().GetLocalFilePath(__sDBPath));
                    return _DBGeneric;
                }
            }


    so in 1 hand:

    I am using that DB all over the App - frequently, so its better to kip that object alive.. (and call destroy on )

    on a 2 hand:

    regarding: GC, performance and memory leek.. will it be better if i just not use the static and call destroy.. over and over again..  i understood that static resources will be release at the end of the app life (domain).. but what about unmanaged resource?

    what will be the best practice in this case?

    of the moment i manage the memory release on app sleep event

            protected override void OnSleep()
            {
                DB_Generic.Dispose();
                DBAsync.Dispose();
                _DBGeneric = null;
                _DB_Async = null;
            }

    this is the dispose

    private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        _DB.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                //GC.SuppressFinalize(this);
            }
    
            //~DB_Generic()
            //{
            //    Dispose(false);
            //}

    or.. paraphrase using dispose with GC.SuppressFinalize(this) in each time i call the DB.. ?  

    thanks




    Selasa, 06 November 2018 10.06

Semua Balasan

  • Why can you use a "using" statment for the connection each time code needs to use the database at the method level? 

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

    <copied>

    When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

    <end>

    Selasa, 06 November 2018 11.25
  • thank you for replay.

    [1]

    database been use frequently and throughout the App 

    [2]

    reducing workflow with creating.. opening and closing the db object.... 

    so.. DB singleton 

    the DB_Generic class implements the database operations (insert, delete...) 

    Selasa, 06 November 2018 12.35
  • thank you for replay.

    [1]

    database been use frequently and throughout the App 

    [2]

    reducing workflow with creating.. opening and closing the db object.... 

    so.. DB singleton 

    the DB_Generic class implements the database operations (insert, delete...) 

    If you were using a Web based solution it would make sense. And even that is questionable, becuase even in Web based solutions the DB connection is made at the method level opened and closed when done.    But for a desktop solution, it's really buying you nothing a couple microseconds if that.

    However, just how much are you implementing SoC in the solution?  You seem  kind of sharp minded. I am just curious. 

    https://en.wikipedia.org/wiki/Separation_of_concerns

    Selasa, 06 November 2018 14.14
  • Hi 2xo1,

    Since your question is more related to Xamarin, you could post a new thread in Xamarin forum for suitable support.

    https://forums.xamarin.com/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Kamis, 08 November 2018 06.24