MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

DOTNET: Memory Optimization in .NET with Object Pooling

Here’s a single, self-contained console app you can run to see and feel the difference between:

  • 🚫 Without Object Pooling (allocate new arrays every time)
  • ✅ With Object Pooling (reuse arrays via ArrayPool<byte>)

It includes:

  • Full Program.cs code
  • Optional .csproj (if you want a drop-in project)
  • Step-by-step how to run
  • How to interpret the results

Works on any modern .NET (7/8/9/10+).


1️⃣ Full Code – Program.cs


2️⃣ Optional – Minimal .csproj (if you want full project file)

Create a file named ObjectPoolingDemo.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>
Code language: HTML, XML (xml)

You can also change net8.0 to net9.0 or later (e.g., net10.0) depending on your installed SDK.


3️⃣ Step-by-Step: How to Run This Demo

Step 1 – Make a new console project

dotnet new console -n ObjectPoolingDemo
cd ObjectPoolingDemo
Code language: JavaScript (javascript)

Step 2 – Replace Program.cs

Open Program.cs in your editor and replace everything with the code from section 1️⃣.

(Optionally replace the autogenerated .csproj with the one in 2️⃣, but not required.)

Step 3 – Build and run in Release

dotnet run -c Release

You’ll see output like:

=========================================
   Object Pooling Demo (ArrayPool<byte>)  
=========================================

Iterations : 1,000,000
BufferSize : 1024 bytes

Warming up JIT (small runs)...
--- Warmup - Without Pooling ---
Iterations: 100,000, BufferSize: 1024 bytes
Time Elapsed : 120 ms
GC Gen0      : 12
GC Gen1      : 1
GC Gen2      : 0
Managed Memory Delta (approx): 3.40 MB
Checksum (ignore, just to keep JIT honest): 123456789

--- Warmup - With Pooling ---
Iterations: 100,000, BufferSize: 1024 bytes
Time Elapsed : 60 ms
GC Gen0      : 2
GC Gen1      : 0
GC Gen2      : 0
Managed Memory Delta (approx): 0.20 MB
Checksum (ignore, just to keep JIT honest): 123456789

=========== REAL TESTS (Release) ==========

--- WITHOUT pooling (new byte[] each time) ---
Iterations: 1,000,000, BufferSize: 1024 bytes
Time Elapsed : 900 ms
GC Gen0      : 100
GC Gen1      : 5
GC Gen2      : 1
Managed Memory Delta (approx): 40.00 MB
Checksum (ignore, just to keep JIT honest): 123456789

--- WITH pooling (ArrayPool<byte>.Shared) ---
Iterations: 1,000,000, BufferSize: 1024 bytes
Time Elapsed : 400 ms
GC Gen0      : 5
GC Gen1      : 0
GC Gen2      : 0
Managed Memory Delta (approx): 2.00 MB
Checksum (ignore, just to keep JIT honest): 123456789
Code language: HTML, XML (xml)

(Exact numbers will differ per machine, but the pattern will be similar.)


4️⃣ How to “Experience” and Interpret the Results

Focus on these lines for each scenario:

  1. Time Elapsed
  2. GC Gen0 / Gen1 / Gen2
  3. Managed Memory Delta (MB)

🔴 Scenario: WITHOUT pooling

  • new byte[bufferSize] is executed 1,000,000 times.
  • That means ~1,024 * 1,000,000 ≈ 1 GB worth of arrays allocated over time.
  • You should see:
    • Higher elapsed time
    • Many more Gen0 collections
    • Possibly some Gen1/Gen2 collections
    • A larger memory delta

This simulates a real-world high-allocation hot path (e.g., per-request/per-message allocations).


🟢 Scenario: WITH pooling (ArrayPool.Shared)

  • Arrays are rented and returned from a shared pool.
  • Only a small number of underlying arrays are actually allocated.
  • Subsequent rents reuse these buffers.

Expected result:

  • Lower elapsed time (less GC interference + fewer allocations)
  • Much fewer Gen0 collections, often 5–10x fewer
  • Gen1/Gen2 collections may drop to 0 or near 0
  • Managed Memory Delta much smaller

You are literally seeing GC pressure decreasing because:

  • Without pooling → allocate, discard, GC must clean up
  • With pooling → allocate a few times, then reuse, GC is mostly idle

5️⃣ Tweaking the Demo to Feel the Effect More

If you want even more dramatic differences:

  • Increase iterations (e.g., 5_000_000)
  • Or increase bufferSize (e.g., 4096 or 16_384)
const int iterations = 5_000_000;
const int bufferSize = 4096;
Code language: JavaScript (javascript)

⚠️ Be careful: this can make the non-pooled version quite heavy on CPU and memory. Good for a demo on a strong machine, but maybe too much on low-end hardware.


6️⃣ How to Understand in Training

You can summarize for your audience:

“In the no pooling scenario, we allocate a fresh buffer for each iteration, forcing the GC to keep cleaning up.
In the pooling scenario, we reuse buffers from ArrayPool<byte>.Shared, dramatically reducing allocations and GC work.
The difference in GC counts and elapsed time is the direct impact of object pooling.”

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

PropRaahi: How a New Property Research Platform Is Tackling India’s Biggest Real Estate Problems

From confusing property documents and hidden costs to unreliable information and buyer uncertainty, PropRaahi wants to make property buying in India more transparent, informed, and buyer-friendly. India’s…

Read More

Why Engineers Click Through Compliance Training – and What DevOps Teams Can Do About It

Ask a room of engineers about their annual compliance module and you will hear the same story: a slide deck on data protection, a few multiple-choice questions,…

Read More

Could An AI Chatbot Help Your Business Deliver Better Customer Support? What To Know About Implementing One

Customers don’t always have the time or patience to wait for answers. From checking delivery times and asking about a service to trying to resolve a problem,…

Read More

How to stop support escalations eating your engineering time

Nobody decides that engineers should do support. It happens one message at a time. A ticket looks odd, L1 isn’t sure, and someone pings the developer who…

Read More

Best Print Driver Management Software for IT Teams in 2027: 7 Tools Compared

Microsoft’s end-of-servicing plan for legacy third-party printer drivers is reshaping how IT teams handle print management. From January 15, 2026, no new printer drivers are published to…

Read More

Switching EOR Providers: How to Protect Your Employees and Keep Work on Track

Changing an employment provider can affect payroll, employee records, benefits and day-to-day support. Without careful planning, even a necessary transition can create uncertainty for international employees. A…

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Skylar Bennett
Skylar Bennett
11 months ago

This article does a great job of showing how object pooling can be used as a practical performance strategy in .NET applications. By reusing objects instead of creating and disposing them repeatedly, developers can lower memory allocations and reduce the overhead of frequent garbage collection. The explanation makes it clear that object pooling is especially useful in high-throughput and long-running services. Overall, the blog helps readers understand when object pooling makes sense and how it can contribute to building stable, high-performance .NET systems.

Jason Mitchell
Jason Mitchell
11 months ago

This article gives a very clear and practical explanation of how object pooling can help .NET applications reuse instances instead of constantly allocating and discarding them — a tactic that can greatly reduce memory churn and improve performance in high-load environments. The examples and scenarios described make it easy to see when object pooling is most beneficial — especially for expensive-to-create objects or frequently used resources — and why it matters for scalability and efficiency. I appreciate how the post balances theory with actionable advice that developers can implement right away. Overall, this is a valuable resource for anyone working on optimizing resource usage and responsiveness in .NET applications.

2
0
Would love your thoughts, please comment.x
()
x