Runtime: What is the purpose of ArrayPool.Create() if there is no limit to what i can rent ?

Created on 29 Jun 2020  路  6Comments  路  Source: dotnet/runtime

i created a custom pool like

ArrayPool<byte> pool = ArrayPool.Create( 1MB , 5 )

This would mean that i am creating a new pool with max array size 1 mb and 5 arrays per bucket.
But if i try to rent array of more than 1 MB , i am able to do so. Also , i can rent more than 5 arrays as it would allocate it from next bucket.
So, how does a custom pool helps specifically?

area-System.Buffers

All 6 comments

Tagging subscribers to this area: @tannergooding
Notify danmosemsft if you want to be subscribed.

It helps in the case that you don't exceed either of those limits. What those limits should be would be revealed via profiling/tuning for a specific application/code path.

@Clockwork-Muse Okay thanks ! That means if at runtime my app tries to somehow rent more than the limit set, then the memory profiling will be different from what is expected ,right?

But if i try to rent array of more than 1 MB , i am able to do so

ArrayPool, when asked for an array larger than the maximum array size, will simply allocate a new array.

https://github.com/dotnet/runtime/blob/a306b03891e447b6f68fe5e664fce8336567ac88/src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs#L105-L109

This is true of the Shared ArrayPool as well. So one instance of where you may want to create your own array pool is where you want to pool array objects larger than the default (which I think is 1024虏). So, you might want to make your own pool if you want to pool very large objects. Or perhaps you only want to pool smaller objects, so you might use a lower size.

@ChahatKumar - Yes, but how much would depend on how often that happens. If you're writing a server where you get millions of messages that are 80-100kb, but every month you get one message that for some reason is > 5Mb, profiling is going to say "look, use 100kb as the limit" (and let that 5Mb array get created/dropped as a one-off). If every 100th message is really large, though, you might want to have two separate pools, one larger pool of smaller objects, and one smaller pool of larger objects.

@vcsjones @Clockwork-Muse Got it ! Thanks :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Drawaes picture Drawaes  路  268Comments

jamesqo picture jamesqo  路  206Comments

hqueue picture hqueue  路  155Comments

fiigii picture fiigii  路  181Comments

ebickle picture ebickle  路  318Comments