ASP.Net object memory?

Soldato
Joined
12 Jan 2004
Posts
6,824
Location
Londinium
Hi guys,

Just wondering if someone could give me some basic advice on how to handle objects in ASP.NET so that memory issues do not appear when under load.

Many thanks.
 
Dispose() them when you are finished using them can help with some types of objects. To be honest though .NET memory management is largely auto-magic. It's very luxurious being able to create a load of objects and totally "forgetting" to free them :D

Also for short lived objects you can use the using(object mine = new object()) {} block. This is basically sugar for doing the same as above.

Note: Never call GC.Collect() in production code.
 
Last edited:
don't forget to close ADO objects (datareader, connection) after using them, otherwise they can't be freed by the garbage collector.

anything else the GC will collect when it's no longer in use.

p.s. why assign a new object to an existing one? the Dispose() method will clear it out won't it?
 
Dr_Evil said:
don't forget to close ADO objects (datareader, connection) after using them, otherwise they can't be freed by the garbage collector.

anything else the GC will collect when it's no longer in use.
Actually the GC will still free such objects if Close() isn't called (because they are no longer referenced anywhere in the program). However as the GC could take hours to come along...... the consequences are obvious. So yeah, always call Close() if the class' documentation suggests to do so.
 
Thanks for the advice guys.

One other quick question though: when deploying your app, do you manually compile the 'cs' files into dlls and deploy those, rather than letting it compile automatically when requested?
 
NathanE said:
Actually the GC will still free such objects if Close() isn't called (because they are no longer referenced anywhere in the program). However as the GC could take hours to come along...... the consequences are obvious. So yeah, always call Close() if the class' documentation suggests to do so.

True, depending of the GC policy. However, i think most of those objects will be eligible for GC when the user session has expired anyways. I think it's just safer practise to close any data-related objects anyways, to reduce the number of connections/threads onto your database server.

As for deploying apps - just use the "Publish Website" option in Visual Studio, that will deploy a pre-compiled version to another location/server with or without the sources.
 
Back
Top Bottom