How the runtime cleans up memory
In C# you create objects and rarely destroy them. The runtime watches for objects nothing refers to any more and reclaims their memory.
What the collector does
- It tracks which objects are still reachable from your running code.
- It reclaims the memory of objects that are no longer reachable.
- It may move remaining objects to keep free memory together.
What it cannot do for you
Garbage collection manages memory, not every resource. A database connection, a file handle or a network stream holds something outside memory, and those must be closed when you finish with them.
C#
using (var reader = command.ExecuteReader())
{
// the reader closes at the end of this block
}Closing a database connection is your job. The using block above is the simplest way to guarantee it, and the data track uses the same pattern throughout.
That completes the language fundamentals. Continue to ASP.NET from scratch to put these ideas on a web page.