Wednesday, February 5, 2025

How .NET API Uses RAM?

✅ Application Start: Code and dependencies are loaded into RAM.

✅ CLR Loads Assemblies: JIT compiles and stores DLLs in RAM.

✅ Memory Allocation: Stack (method calls) & Heap (objects, DI, cache).

✅ Caching (Boosts Speed!): Frequently accessed data is stored in RAM.

✅ Handling HTTP Requests: Kestrel/IIS processes requests in memory.

✅ Garbage Collection: Frees up unused objects, optimizing RAM.

✅ (Optional) In-Memory Database: Uses RAM for high-speed transactions.


🔹 Complete Process in a Simple Diagram!


+----------------------------------------------------+

| 🟢 APPLICATION START (Loaded in RAM) |

| - Run API: (dotnet run / IIS / Kestrel) |

| - .NET Runtime (CLR) loads code into RAM |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| 🔄 CLR LOADS ASSEMBLIES (Stored in RAM) |

| - JIT (Just-In-Time) compiles code |

| - Dependencies & DLLs loaded into memory (RAM) |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| 💾 MEMORY ALLOCATION (RAM Usage) |

| - Stack: Stores method calls, local variables |

| - Heap: Stores objects, services, cache data |

| - DI Container: Manages service lifetimes |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| ⚡ CACHING (Improves Speed, Uses RAM) |

| - In-Memory Cache (MemoryCache, Singleton) |

| - Distributed Cache (Redis, NCache) (Optional) |

| - Reduces Database Calls, Increases Performance |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| 🌐 HANDLING HTTP REQUESTS |

| - Kestrel/IIS Listens for Requests |

| - Middleware (Auth, Logging, Compression) |

| - Controllers Execute Business Logic |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| 🗑️ GARBAGE COLLECTION (Frees RAM) |

| - Gen 0: Short-lived objects (frequent cleanup) |

| - Gen 1 & 2: Long-lived objects (less cleanup) |

| - Optimizes RAM usage, prevents memory leaks |

+----------------------------------------------------+

|

v

+----------------------------------------------------+

| 🛢️ (OPTIONAL) IN-MEMORY DATABASE |

| - EF Core In-Memory / SQLite (Stored in RAM) |

| - Fast Reads/Writes, No Disk I/O |

+----------------------------------------------------+

💡 Why This Matters?

🔹 Faster performance with in-memory caching.

🔹 Reduced database calls, improving scalability.

🔹 Optimized RAM usage via Garbage Collection.


No comments:

Post a Comment

How to Do Cross-Browser Testing on Windows — Including Safari via Playwright

To test follow few steps: Step 1. Install node Step 2. create Folder and run these commands commands Copy Code ...