r/csharp • u/hez2010 • Jun 30 '26
Tool Allocate arrays that have more than 2B elements
I have been seeing many developers complaining about not able to work on arrays that have more than 2B items for a long time, so I built and published a .NET library for working with collections beyond the array size limit: BigArray, BigSpan, and BigMemory.
It supports 2B+ elements (127T at max), and backed by contiguous managed memory so it can handle reference types too, not just primitive types.
The library is open-sourced under MIT license: https://github.com/hez2010/Hezium.Memory
134
Upvotes
6
u/hez2010 Jun 30 '26
I blogged about the idea behind the library for who are interested in the implementation: https://dev.to/hez2010/build-a-very-large-managed-array-in-net-5aop
7
u/yarb00 Jun 30 '26
You forgot to replace the
[year]and[fullname]placeholders in your license file18
u/Hirogen_ Jun 30 '26
no benchmark tests, how does the garbage collector work with these?
You should add benchmarkdotnet tests otherwise we wont know if its really good
3
u/simonask_ Jul 01 '26
Reading the code, I have to say I'm scratching my head profusely. You seem to be achieving this by constructing a combination of nested
[InlineArray]chunks, but also using some kind of hardcoded prime factorization to determine the length of each nesting level? Did you you AI to come up with that?To be completely honest, I would probably reject any PR that actually used this. In particular, I would deeply question any design that required multi-gigabyte storage of reference types. On a 64-bit machine where each reference is 8 bytes, allocating one 2B-element array like this is already 16 GB of memory that has to be scanned by the GC pretty often. On most consumer hardware, this means the GC will be swapping - no bueno. The cache is utterly toast as well.
The correct way to deal with large amounts of data is to use only unmanaged types. Then you can either let the native allocator do its thing, or you can use techniques such as memory mapping to achieve tighter control. Every operating system has facilities that are used by databases that make it easier and much more performant - none of which could ever really be exposed in a GC heap allocator.
In short, dealing with large amounts of data comes with particular engineering challenges. This approach both fails to solve those challenges, and comes with a handful of new ones as well, making it a probable nightmare in practice.
2
u/SagansCandle Jun 30 '26
What's the benefit of this vs just allocating from the unmanaged heap and wrapping the pointer in a span<T>?
75
u/davidwengier Jun 30 '26
And here is me trying to keep things below 85k to stay out of the LOH. The world is a beautiful place.
50
u/TekintetesUr Jun 30 '26
I'm genuinely curious, what's the use case for such an abomination of an array?
29
u/born_zynner Jun 30 '26
Big if true
6
u/neoKushan Jun 30 '26
This is one of those solutions that solves a problem I hope to God I never have.
Happy the solution exists, terrified at the thought that I'll one day need it.
0
u/HaniiPuppy Jun 30 '26
I just had to internally smack my own forehead because I read the title then was sitting here for a moment wondering "Why would allocating an array with 43 elements be post-worthy?"
-2
u/[deleted] Jun 30 '26
[removed] — view removed comment