Should I explicitly destroy objects

Discussion in 'ASP .Net' started by =?Utf-8?B?dnZlbms=?=, Nov 9, 2005.

  1. Hello:

    I just wrote my first ASP.Net application. It worked fine on my machine and
    when I put into production, the ASP.Net process reaches 50% quite fast and
    then the system does not work anymore until I kill that process.

    Obviously, this is not acceptable.

    Looking back, I do not destroy any objects in my form. Would that be the
    reasn why the application breaks down?

    I could explicitly set objects to nothing in


    I would like to know
     
    =?Utf-8?B?dnZlbms=?=, Nov 9, 2005
    #1
    1. Advertising

  2. No you don't need to explicitly destroy managed objects. However, unmanaged
    objects need to be released such as database connections, file handles etc
    since the managed wrapper for these wrap around unmanaged objects, .NET does
    not know how to free these objects.

    I'd venture a guess that memory is not the cause of your application falling
    over dead, you likely have some other nasty issue.

    --
    Regards,
    Alvin Bruney [MVP ASP.NET]

    [Shameless Author plug]
    The Microsoft Office Web Components Black Book with .NET
    Now Available @ www.lulu.com/owc
    Forth-coming VSTO.NET - Wrox/Wiley 2006
    -------------------------------------------------------



    "vvenk" <> wrote in message
    news:...
    > Hello:
    >
    > I just wrote my first ASP.Net application. It worked fine on my machine

    and
    > when I put into production, the ASP.Net process reaches 50% quite fast and
    > then the system does not work anymore until I kill that process.
    >
    > Obviously, this is not acceptable.
    >
    > Looking back, I do not destroy any objects in my form. Would that be the
    > reasn why the application breaks down?
    >
    > I could explicitly set objects to nothing in
    >
    >
    > I would like to know
     
    Alvin Bruney - ASP.NET MVP, Nov 9, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?dnZlbms=?=

    Chris Botha Guest

    As you said, this is not acceptable.
    First place to look at I would say is to ensure that you close a connection
    to the database when you are done with it. I also call Dispose on it after
    Close for extra luck.

    "vvenk" <> wrote in message
    news:...
    > Hello:
    >
    > I just wrote my first ASP.Net application. It worked fine on my machine
    > and
    > when I put into production, the ASP.Net process reaches 50% quite fast and
    > then the system does not work anymore until I kill that process.
    >
    > Obviously, this is not acceptable.
    >
    > Looking back, I do not destroy any objects in my form. Would that be the
    > reasn why the application breaks down?
    >
    > I could explicitly set objects to nothing in
    >
    >
    > I would like to know
     
    Chris Botha, Nov 9, 2005
    #3
  4. Chris:

    I tried destroying these objects in the page_unload event but that event is
    triggered right after the page_load event!

    Which event is the appropriate one for destroying these objects?

    "Chris Botha" wrote:

    > As you said, this is not acceptable.
    > First place to look at I would say is to ensure that you close a connection
    > to the database when you are done with it. I also call Dispose on it after
    > Close for extra luck.
    >
    > "vvenk" <> wrote in message
    > news:...
    > > Hello:
    > >
    > > I just wrote my first ASP.Net application. It worked fine on my machine
    > > and
    > > when I put into production, the ASP.Net process reaches 50% quite fast and
    > > then the system does not work anymore until I kill that process.
    > >
    > > Obviously, this is not acceptable.
    > >
    > > Looking back, I do not destroy any objects in my form. Would that be the
    > > reasn why the application breaks down?
    > >
    > > I could explicitly set objects to nothing in
    > >
    > >
    > > I would like to know

    >
    >
    >
     
    =?Utf-8?B?dnZlbms=?=, Nov 9, 2005
    #4
  5. =?Utf-8?B?dnZlbms=?=

    Chris Botha Guest

    My guess would be that you see the page_unload event so fast because your
    computer is snappy. The page_unload event is a good place to destroy them,
    if you mean conn.Close() and conn.Dispose(), because just setting them to
    nothing won't help as in the old VB days.


    "vvenk" <> wrote in message
    news:...
    > Chris:
    >
    > I tried destroying these objects in the page_unload event but that event
    > is
    > triggered right after the page_load event!
    >
    > Which event is the appropriate one for destroying these objects?
    >
    > "Chris Botha" wrote:
    >
    >> As you said, this is not acceptable.
    >> First place to look at I would say is to ensure that you close a
    >> connection
    >> to the database when you are done with it. I also call Dispose on it
    >> after
    >> Close for extra luck.
    >>
    >> "vvenk" <> wrote in message
    >> news:...
    >> > Hello:
    >> >
    >> > I just wrote my first ASP.Net application. It worked fine on my machine
    >> > and
    >> > when I put into production, the ASP.Net process reaches 50% quite fast
    >> > and
    >> > then the system does not work anymore until I kill that process.
    >> >
    >> > Obviously, this is not acceptable.
    >> >
    >> > Looking back, I do not destroy any objects in my form. Would that be
    >> > the
    >> > reasn why the application breaks down?
    >> >
    >> > I could explicitly set objects to nothing in
    >> >
    >> >
    >> > I would like to know

    >>
    >>
    >>
     
    Chris Botha, Nov 9, 2005
    #5
  6. You cannot destroy .Net objects. They are destroyed when Garbage Collection
    destroys them.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Complex things are made up of
    Lots of simple things.

    "vvenk" <> wrote in message
    news:...
    > Chris:
    >
    > I tried destroying these objects in the page_unload event but that event
    > is
    > triggered right after the page_load event!
    >
    > Which event is the appropriate one for destroying these objects?
    >
    > "Chris Botha" wrote:
    >
    >> As you said, this is not acceptable.
    >> First place to look at I would say is to ensure that you close a
    >> connection
    >> to the database when you are done with it. I also call Dispose on it
    >> after
    >> Close for extra luck.
    >>
    >> "vvenk" <> wrote in message
    >> news:...
    >> > Hello:
    >> >
    >> > I just wrote my first ASP.Net application. It worked fine on my machine
    >> > and
    >> > when I put into production, the ASP.Net process reaches 50% quite fast
    >> > and
    >> > then the system does not work anymore until I kill that process.
    >> >
    >> > Obviously, this is not acceptable.
    >> >
    >> > Looking back, I do not destroy any objects in my form. Would that be
    >> > the
    >> > reasn why the application breaks down?
    >> >
    >> > I could explicitly set objects to nothing in
    >> >
    >> >
    >> > I would like to know

    >>
    >>
    >>
     
    Kevin Spencer, Nov 9, 2005
    #6
  7. Kevin:

    Initially I was not destroying exactly for that reason. But when the Asp.Net
    process was taking 50% of the memory with two users using the applictaion, I
    thought I should explicitly destroy these objects.

    Are you implying that the changes I have made will have no impact on the
    memory problem?

    "Kevin Spencer" wrote:

    > You cannot destroy .Net objects. They are destroyed when Garbage Collection
    > destroys them.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > ..Net Developer
    > Complex things are made up of
    > Lots of simple things.
    >
    > "vvenk" <> wrote in message
    > news:...
    > > Chris:
    > >
    > > I tried destroying these objects in the page_unload event but that event
    > > is
    > > triggered right after the page_load event!
    > >
    > > Which event is the appropriate one for destroying these objects?
    > >
    > > "Chris Botha" wrote:
    > >
    > >> As you said, this is not acceptable.
    > >> First place to look at I would say is to ensure that you close a
    > >> connection
    > >> to the database when you are done with it. I also call Dispose on it
    > >> after
    > >> Close for extra luck.
    > >>
    > >> "vvenk" <> wrote in message
    > >> news:...
    > >> > Hello:
    > >> >
    > >> > I just wrote my first ASP.Net application. It worked fine on my machine
    > >> > and
    > >> > when I put into production, the ASP.Net process reaches 50% quite fast
    > >> > and
    > >> > then the system does not work anymore until I kill that process.
    > >> >
    > >> > Obviously, this is not acceptable.
    > >> >
    > >> > Looking back, I do not destroy any objects in my form. Would that be
    > >> > the
    > >> > reasn why the application breaks down?
    > >> >
    > >> > I could explicitly set objects to nothing in
    > >> >
    > >> >
    > >> > I would like to know
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?dnZlbms=?=, Nov 10, 2005
    #7
  8. > Initially I was not destroying exactly for that reason. But when the
    > Asp.Net
    > process was taking 50% of the memory with two users using the applictaion,
    > I
    > thought I should explicitly destroy these objects.


    An assumption on your part. First, 50% is not an amount, but a ratio, so I
    have no idea how much memory you're talking about. Second, assuming that the
    app *is* consuming an excessive amount of memory (and that is quite the
    assumption; .Net manages memory quite well, but will not discard it
    immediately), the real problem would be to determine exactly what is causing
    the excessive use of memory. Let's say that your car is using too much gas.
    Would you assume that changing the spark plugs would fix the problem, or
    would you identify all of the possible causes of excessive gas use, and
    check each related component to find out which one was misbehaving and why,
    and then fix that component? In other words, some diagnostics are necessary
    before any attempt to fix the issue is made.

    > Are you implying that the changes I have made will have no impact on the
    > memory problem?


    If I implied that, I would be making an assumption myself. First, I have no
    idea what changes you have made. You have talked about "destroying objects,"
    but as I told you, that is not something you can do. They are managed, and
    the option to destroy them is not one available to the developer. So, since
    you say that "I tried destroying these objects in the page_unload event,"
    and it is not possible to destroy .Net objects, I can't presume to know what
    you did to your code as an attempt to destroy them.

    I could presuppose that you assigned a null (Nothing) value to them, but if
    so, that would be of no effect. For one thing, a variable or field is simply
    a container for an object. It is not an object itself. When you set the
    value of a variable, you are only saying to the computer, "when I reference
    this name, I am referencing that object." When you set the value of a
    variable to Nothing, you are saying to the computer, "when I reference this
    name, I am not referencing anything."

    So, in order to help you, you will need to fill in the blanks where I have
    indicated. How much memory are we talking about? What sort of objects are
    you using, and how are you using them?

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Complex things are made up of
    Lots of simple things.

    "vvenk" <> wrote in message
    news:...
    > Kevin:
    >
    > Initially I was not destroying exactly for that reason. But when the
    > Asp.Net
    > process was taking 50% of the memory with two users using the applictaion,
    > I
    > thought I should explicitly destroy these objects.
    >
    > Are you implying that the changes I have made will have no impact on the
    > memory problem?
    >
    > "Kevin Spencer" wrote:
    >
    >> You cannot destroy .Net objects. They are destroyed when Garbage
    >> Collection
    >> destroys them.
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> ..Net Developer
    >> Complex things are made up of
    >> Lots of simple things.
    >>
    >> "vvenk" <> wrote in message
    >> news:...
    >> > Chris:
    >> >
    >> > I tried destroying these objects in the page_unload event but that
    >> > event
    >> > is
    >> > triggered right after the page_load event!
    >> >
    >> > Which event is the appropriate one for destroying these objects?
    >> >
    >> > "Chris Botha" wrote:
    >> >
    >> >> As you said, this is not acceptable.
    >> >> First place to look at I would say is to ensure that you close a
    >> >> connection
    >> >> to the database when you are done with it. I also call Dispose on it
    >> >> after
    >> >> Close for extra luck.
    >> >>
    >> >> "vvenk" <> wrote in message
    >> >> news:...
    >> >> > Hello:
    >> >> >
    >> >> > I just wrote my first ASP.Net application. It worked fine on my
    >> >> > machine
    >> >> > and
    >> >> > when I put into production, the ASP.Net process reaches 50% quite
    >> >> > fast
    >> >> > and
    >> >> > then the system does not work anymore until I kill that process.
    >> >> >
    >> >> > Obviously, this is not acceptable.
    >> >> >
    >> >> > Looking back, I do not destroy any objects in my form. Would that be
    >> >> > the
    >> >> > reasn why the application breaks down?
    >> >> >
    >> >> > I could explicitly set objects to nothing in
    >> >> >
    >> >> >
    >> >> > I would like to know
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Kevin Spencer, Nov 10, 2005
    #8
  9. =?Utf-8?B?dnZlbms=?=

    Chris Botha Guest

    If you are using database connections you have to close them and as I said I
    also Dispose the connection after Close, don't know how important the
    Dispose is. If you don't, then the connection pool will grow.
    Just setting objects to nothing won't help.

    "vvenk" <> wrote in message
    news:...
    > Kevin:
    >
    > Initially I was not destroying exactly for that reason. But when the
    > Asp.Net
    > process was taking 50% of the memory with two users using the applictaion,
    > I
    > thought I should explicitly destroy these objects.
    >
    > Are you implying that the changes I have made will have no impact on the
    > memory problem?
    >
    > "Kevin Spencer" wrote:
    >
    >> You cannot destroy .Net objects. They are destroyed when Garbage
    >> Collection
    >> destroys them.
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> ..Net Developer
    >> Complex things are made up of
    >> Lots of simple things.
    >>
    >> "vvenk" <> wrote in message
    >> news:...
    >> > Chris:
    >> >
    >> > I tried destroying these objects in the page_unload event but that
    >> > event
    >> > is
    >> > triggered right after the page_load event!
    >> >
    >> > Which event is the appropriate one for destroying these objects?
    >> >
    >> > "Chris Botha" wrote:
    >> >
    >> >> As you said, this is not acceptable.
    >> >> First place to look at I would say is to ensure that you close a
    >> >> connection
    >> >> to the database when you are done with it. I also call Dispose on it
    >> >> after
    >> >> Close for extra luck.
    >> >>
    >> >> "vvenk" <> wrote in message
    >> >> news:...
    >> >> > Hello:
    >> >> >
    >> >> > I just wrote my first ASP.Net application. It worked fine on my
    >> >> > machine
    >> >> > and
    >> >> > when I put into production, the ASP.Net process reaches 50% quite
    >> >> > fast
    >> >> > and
    >> >> > then the system does not work anymore until I kill that process.
    >> >> >
    >> >> > Obviously, this is not acceptable.
    >> >> >
    >> >> > Looking back, I do not destroy any objects in my form. Would that be
    >> >> > the
    >> >> > reasn why the application breaks down?
    >> >> >
    >> >> > I could explicitly set objects to nothing in
    >> >> >
    >> >> >
    >> >> > I would like to know
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Chris Botha, Nov 10, 2005
    #9
    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. Replies:
    2
    Views:
    280
  2. Ook
    Replies:
    2
    Views:
    344
  3. beclan
    Replies:
    12
    Views:
    933
    James Kanze
    Nov 2, 2009
  4. K. Frank
    Replies:
    7
    Views:
    287
  5. Chris Angelico
    Replies:
    0
    Views:
    56
    Chris Angelico
    May 18, 2014
Loading...

Share This Page