Results 1 to 3 of 3

Thread: [2.0]More Create or more Persistance

  1. #1

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    [2.0]More Create or more Persistance

    We are in the middle of a design and are starting to split on a couple issues. I was hoping to get some input or maybe some articles that might address some of our confusion.

    The main point is, is it better to instantiate and destroy objects, or keep them in memory for the life of the program (or a "long" time whatever that is) ?

    One camp here argues that .Net takes care of all this objects creation and destruction, and so just do what you need, when you need to.

    The other camp argues that it's better to instatiate once, and keep what you need "handy". Especially if these objects would require stuff like database access or file access on creation or implementation.

    Any insight would be great.
    Merry Christmas

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0]More Create or more Persistance

    Quote Originally Posted by JPicasso
    The main point is, is it better to instantiate and destroy objects, or keep them in memory for the life of the program (or a "long" time whatever that is) ?
    This completely depends on your object. Do you need it for the life of your program? If so, keep it. If not, destroy it.

    It should become obvious what variables you need or don't need for the life of an application.
    Quote Originally Posted by JPicasso
    One camp here argues that .Net takes care of all this objects creation and destruction, and so just do what you need, when you need to.
    I don't like this camp. The GarbageCollector does NOT clean up objects immediately. There is no guarentee that the garbage collector will even clean up objects until it wants to.

    Any object that inherits the IDisposable interface should be palced inside of a using statement
    VB Code:
    1. using (StreamWriter fout = new StreamWriter("Dunno.txt")){
    2.       //Code Goes Here
    3. }
    This way it will ensure that the StreamWriter is cleaned up immediately and it will ensure that the Stream is closed. If you just let .Net do everything for you, not only can you cause bugs that are almost impossible to debug, but it's poor programming.
    Quote Originally Posted by JPicasso
    The other camp argues that it's better to instatiate once, and keep what you need "handy". Especially if these objects would require stuff like database access or file access on creation or implementation.
    Keep "handy"? You should know if you'll need something again later. If you do and it's more efficient to keep it around then keep it. Something like a database connection may not be a good idea to keep active the whole time though.

    I'd say create the variable when you need it. You should avoid global variables anyway.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0]More Create or more Persistance

    This is .NET, not VB6.

    Use your objects only when you need them, especially database objects. Bandwidth is not free, not even in internal environments.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width