Garbage collection

Discussion in 'ASP .Net' started by luigi, Sep 17, 2006.

  1. luigi

    luigi Guest

    If i dispose all object in my vb.net c# code what perfomances advantage
    i have?

    mant thanks
     
    luigi, Sep 17, 2006
    #1
    1. Advertising

  2. luigi

    Jon Shemitz Guest

    luigi wrote:

    > If i dispose all object in my vb.net c# code what perfomances advantage
    > i have?


    As written, you get a performance hit, or a negative performance
    advantage. Disposing of an object does not reclaim memory, nor does it
    hasten garbage collection; writing and calling an empty Dispose method
    merely wastes space and time.

    Otoh, if you don't really mean "all object" but actually mean "all
    objects that implement IDispose" then you are closing resources when
    you are done with them, and avoiding "finalization costs."
    Finalization is expensive because a dead object must be brought back
    to life, promoted to the next generation, and relocated; it then
    sticks around, taking up memory, until the next higher-level
    collection. So, by calling Dispose on every object that implements it,
    you are reducing memory consumption and garbage collection costs.

    --

    ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
    Delphi skills make .NET easy to learn Great reviews & good sales.
     
    Jon Shemitz, Sep 17, 2006
    #2
    1. Advertising

  3. luigi

    Scott M. Guest

    > So, by calling Dispose on every object that implements it,
    > you are reducing memory consumption and garbage collection costs.


    You mean you are increasing memory consumption and garbage collection costs,
    not reducing them.
     
    Scott M., Sep 25, 2006
    #3
  4. luigi

    Jon Shemitz Guest

    "Scott M." wrote:

    > > So, by calling Dispose on every object that implements it,
    > > you are reducing memory consumption and garbage collection costs.

    >
    > You mean you are increasing memory consumption and garbage collection costs,
    > not reducing them.


    No, I meant what I said. Invoking IDispose allows data to be closed
    when you're done with, and thus avoids the use of the finalizer. The
    finalizer is a fail-safe, but using it adds (avoidable) relocation
    costs and means that the gc has to look at the dead object (at least)
    twice.

    --

    ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
    Delphi skills make .NET easy to learn Great reviews & good sales.
     
    Jon Shemitz, Sep 25, 2006
    #4
  5. luigi

    Scott M. Guest

    Yes, but calling Dispose on every class that exposes it will cause
    additional CPU costs, not less.

    "Jon Shemitz" <> wrote in message
    news:...
    > "Scott M." wrote:
    >
    >> > So, by calling Dispose on every object that implements it,
    >> > you are reducing memory consumption and garbage collection costs.

    >>
    >> You mean you are increasing memory consumption and garbage collection
    >> costs,
    >> not reducing them.

    >
    > No, I meant what I said. Invoking IDispose allows data to be closed
    > when you're done with, and thus avoids the use of the finalizer. The
    > finalizer is a fail-safe, but using it adds (avoidable) relocation
    > costs and means that the gc has to look at the dead object (at least)
    > twice.
    >
    > --
    >
    > .NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
    > Delphi skills make .NET easy to learn Great reviews & good sales.
     
    Scott M., Sep 27, 2006
    #5
  6. luigi

    Jon Shemitz Guest

    "Scott M." wrote:

    > Yes, but calling Dispose on every class that exposes it will cause
    > additional CPU costs, not less.


    No, in most cases it's spending time to save time. When an IDispose
    class has a finalizer, its Dispose methods typically runs the exact
    same code that the finalizer runs, plus it calls GC.SuppressFinalize.
    This is saving all the CPU costs involved in running the finalizer,
    plus it allows the memory to be reused sooner.

    When an IDispose class does not have a finalizer, it usually has
    private reference(s) to handle class(es) that do have a finalizer, so
    the call to Dispose allows it to close its handle(s), thus (again)
    saving all the CPU costs involved in running the handle finalizer(s),
    plus allowing the memory to be reused sooner.

    The only time calling Dispose doesn't save time is with the minority
    of IDispose implementations that aren't avoiding a finalizer, that are
    doing something like restoring the GUI cursor or noting how long it's
    been since a stopwatch class has been created.

    > >> > So, by calling Dispose on every object that implements it,
    > >> > you are reducing memory consumption and garbage collection costs.
    > >>
    > >> You mean you are increasing memory consumption and garbage collection
    > >> costs,
    > >> not reducing them.

    > >
    > > No, I meant what I said. Invoking IDispose allows data to be closed
    > > when you're done with, and thus avoids the use of the finalizer. The
    > > finalizer is a fail-safe, but using it adds (avoidable) relocation
    > > costs and means that the gc has to look at the dead object (at least)
    > > twice.


    --

    ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
    Delphi skills make .NET easy to learn Great reviews & good sales.
     
    Jon Shemitz, Sep 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laser Lu

    Garbage Collection and Manage Code?

    Laser Lu, Jan 26, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    721
    Gaurav Khanna [C# MVP]
    Jan 27, 2004
  2. Cheung, Jeffrey Jing-Yen
    Replies:
    3
    Views:
    1,554
    Cheung, Jeffrey Jing-Yen
    Jul 10, 2003
  3. IDoNothing

    Garbage Collection in ASP.NET

    IDoNothing, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    8,374
    cjohn
    Aug 20, 2009
  4. Replies:
    1
    Views:
    442
    mrstephengross
    Jul 25, 2005
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page