Should I explicitly destroy objects

G

Guest

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
 
A

Alvin Bruney - ASP.NET MVP

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
 
C

Chris Botha

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.
 
G

Guest

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?
 
C

Chris Botha

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.
 
K

Kevin Spencer

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.
 
G

Guest

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?
 
K

Kevin Spencer

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.
 
C

Chris Botha

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top