Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

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.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.