#924 – You Shouldn’t Normally Worry about Memory Management

Because the Common Language Runtime (CLR) implements automatic memory management¬†for heap-based objects, you generally don’t need to worry about memory management for objects that your code creates. ¬†Because the Garbage Collector (GC) takes care of automatically deleting objects that are no longer being referenced, you don’t need to worry about deleting objects or removing references to them.

Your code explicitly creates objects, but doesn’t need to worry about destroying them, or about releasing memory for objects no longer in use.

Your code can freely:

  • Create objects (instances of types)
  • Create references to objects
  • Change reference-type variables to refer to other objects
  • Allow objects to contain references to other objects

Your code typically will not:

  • Dereference objects (e.g. by setting a reference-typed variable to null)
  • Keep track of whether objects have been deleted or not
  • Interact with the Garbage Collector to impact how objects are destroyed