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 make Scheduler with Azure Queue using .net

I created Two methods one for schedule and log in db and second method is related to reschedule message. 1. Schedule and Reschedu...