Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Module 6: .NET Runtime & Memory Management

This module takes you “under the hood.” To be a senior developer, you must understand not just how to write code, but how that code interacts with the physical hardware (RAM and CPU). This is the difference between an app that runs smoothly and one that crashes with an OutOfMemoryException.

1. CLR Architecture (The Engine)

The Common Language Runtime (CLR) is the “virtual machine” component of .NET. It sits between your compiled code and the Operating System.

  • Managed Code: Code written in C# that runs under the control of the CLR. The CLR handles the “dirty work” like security, memory management, and type safety.
  • Key Responsibilities:
    • Class Loader: Loads the compiled .dll files into memory.
    • JIT (Just-In-Time) Compiler: Converts the Intermediate Language (IL) into machine code that your specific CPU (Intel, AMD, ARM) can execute.
    • Garbage Collector: Manages the memory lifecycle.

2. Value vs. Reference Types

Understanding how data is stored is critical for performance tuning.

  • Value Types (int, bool, struct, enum): These hold the actual data. They are usually stored on the Stack. When you pass them to a method, a copy of the data is created.
  • Reference Types (string, class, interface, delegate): These store a “memory address” (a pointer) to where the actual data lives. They are stored on the Heap. When you pass them to a method, you are passing the address, not the data itself.

3. Stack vs. Heap

The computer’s RAM is divided into two main areas for your program:

The Stack (Fast & Organized)

  • How it works: Like a physical stack of books. It follows LIFO (Last-In, First-Out).
  • Scope: When a method finishes, everything it stored on the Stack is instantly wiped away.
  • Speed: Extremely fast because the memory is managed automatically by the CPU.

The Heap (Large & Flexible)

  • How it works: Like a messy warehouse. Objects can be scattered anywhere.
  • Scope: Objects stay here as long as something is pointing to them.
  • Management: This is where the Garbage Collector operates. It is slower than the Stack but can hold much larger amounts of data.

4. Garbage Collection (GC)

In languages like C++, you have to manually delete objects from memory. In C#, the Garbage Collector does it for you. It uses a “Generational” approach to be efficient:

  1. Generation 0: New, short-lived objects (like local variables in a loop). The GC checks this very often because these objects die quickly.
  2. Generation 1: A “buffer” zone for objects that survived one GC cycle.
  3. Generation 2: Long-lived objects (like a static Database Connection). The GC checks this rarely because it assumes these will be around for a while.

The “Stop-the-World” Event: When the GC runs a full cleanup, it sometimes pauses your application for a few milliseconds to move memory around. This is why high-frequency trading apps or games require “GC Tuning.”


5. Async Memory Behavior

Asynchronous programming (async/await) changes how memory is handled.

  • The Problem: Normally, when a method finishes, its Stack memory is cleared. But an async method might “pause” while waiting for a database and resume later.
  • The Solution (State Machines): When you use await, the compiler transforms your method into a State Machine. It moves the local variables from the Stack to the Heap so they survive while the method is “waiting.”

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥