<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d" -->

---
title: C# Tip: Improve memory allocation by initializing...
description: Initializing the capacity of .NET collections like List<T> and HashSet<T> with a known size avoids repeated internal resizing, improving both execution time...
canonical: https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: C# Tip: Improve memory allocation by initializing collection size | daily.dev
og:description: Initializing the capacity of .NET collections like List<T> and HashSet<T> with a known size avoids repeated internal resizing, improving both execution time...
og:url: https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d
og:image: https://api.daily.dev/og/posts/aNHC3vS8D.png
og:image:alt: C# Tip: Improve memory allocation by initializing collection size
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Tip: Improve memory allocation by initializing collection size

**[Code4IT](https://daily.dev/sources/code4it)** · 4 min read · 0 upvotes · 0 comments

## Summary

Initializing the capacity of .NET collections like List<T> and HashSet<T> with a known size avoids repeated internal resizing, improving both execution time and memory allocation. Benchmarks run with BenchmarkDotNet on .NET 8.0.100-rc.1 across sizes of 100 to 100,000 items show roughly 1.7-2x faster execution and 2-3.3x less memory allocated when the collection is pre-sized compared to default initialization.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.code4it.dev/csharptips/initialize-collection-size>

## Questions this post answers

### Does initializing a C# List with an expected capacity actually improve performance?

Yes, benchmarks using BenchmarkDotNet on .NET 8.0.100-rc.1 show that initializing a List<int> with new List<int>(size) instead of new List<int>() yields about 1.7x faster execution on average and roughly 2.5x less memory allocated across sizes from 100 to 100,000 items, because the list avoids repeated internal array resizing.

_Developers tuning hot-path collection code can find similar benchmark-backed C# tips curated on daily.dev._

### How much memory does an uninitialized HashSet waste compared to a pre-sized one in C#?

An uninitialized HashSet<int> allocates roughly 2.8x to 3.3x more memory than one created with new HashSet<int>(size) specifying the expected element count, based on benchmarks spanning sizes of 100 to 100,000 items. Execution time also improves by about 1.9x to 2.7x when the capacity is specified upfront.

_Those optimizing .NET memory footprints can track more allocation-focused C# benchmarks via daily.dev._

## Similar posts on daily.dev

- [Making foreach on an IEnumerable allocation-free using reflection and dynamic methods](https://daily.dev/posts/making-foreach-on-an-ienumerable-allocation-free-using-reflection-and-dynamic-methods-cmewcz0u9) · .NET Escapades · 1 upvotes · 0 comments
- [The Fasting Developer: Memory Discipline and GC Awareness in .NET](https://daily.dev/posts/the-fasting-developer-memory-discipline-and-gc-awareness-in-net-5yq85h552) · We Are .NET · 12 upvotes · 0 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"C# Tip: Improve memory allocation by initializing collection size","url":"https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d"},"datePublished":"2026-10-04T13:07:16.534Z","dateModified":"2026-10-04T13:15:34.785Z","description":"Initializing the capacity of .NET collections like List<T> and HashSet<T> with a known size avoids repeated internal resizing, improving both execution time...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9a078adbc395f13775711d170973d7e6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9a078adbc395f13775711d170973d7e6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Code4IT","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Code4IT","logo":"https://media.daily.dev/image/upload/s--OaaG4G10--/f_auto,q_auto/v1791119049/logos/code4it?_a=BAMAMicg0","url":"https://daily.dev/sources/code4it"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,.net,c#","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Code4IT","item":"https://daily.dev/sources/code4it"},{"@type":"ListItem","position":3,"name":"C# Tip: Improve memory allocation by initializing collection size"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/c-tip-improve-memory-allocation-by-initializing-collection-size-anhc3vs8d#faq","mainEntity":[{"@type":"Question","name":"Does initializing a C# List with an expected capacity actually improve performance?","acceptedAnswer":{"@type":"Answer","text":"Yes, benchmarks using BenchmarkDotNet on .NET 8.0.100-rc.1 show that initializing a List<int> with new List<int>(size) instead of new List<int>() yields about 1.7x faster execution on average and roughly 2.5x less memory allocated across sizes from 100 to 100,000 items, because the list avoids repeated internal array resizing. Developers tuning hot-path collection code can find similar benchmark-backed C# tips curated on daily.dev."}},{"@type":"Question","name":"How much memory does an uninitialized HashSet waste compared to a pre-sized one in C#?","acceptedAnswer":{"@type":"Answer","text":"An uninitialized HashSet<int> allocates roughly 2.8x to 3.3x more memory than one created with new HashSet<int>(size) specifying the expected element count, based on benchmarks spanning sizes of 100 to 100,000 items. Execution time also improves by about 1.9x to 2.7x when the capacity is specified upfront. Those optimizing .NET memory footprints can track more allocation-focused C# benchmarks via daily.dev."}}]}
```

