Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

C# is very intelligent when it comes to managing its memory. Like Java and Python, it automatically manages memory; heap memory is managed automatically and stack data generally is, as well. For example, it will not allocate memory for an object or generic that is never used. On 32-bit systems, each program has 2GB of virtual address space; this is the space we manipulate–never the memory directly. Among other advantages, automatic memory management avoids problems such as dangling pointers and memory leaks

Garbage collection is done when the system has low physical memory or when the heap's allocated objects take up more than the allowed memory level. Automatic collection can also be done, but has almost no practical applications. C# has three "generations" that are used in garbage collection: 0 for short-lived data such as temporary variables, 1 (a buffer between short and long-lived data), and 2 for long-lived data such as static data used throughout the program.

...