runtime icon indicating copy to clipboard operation
runtime copied to clipboard

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

Open ChahatKumar opened this issue 6 years ago • 6 comments

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?

ChahatKumar avatar Jun 29 '20 19:06 ChahatKumar

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

ghost avatar Jun 29 '20 19:06 ghost

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 avatar Jun 29 '20 19:06 Clockwork-Muse

@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?

ChahatKumar avatar Jun 29 '20 19:06 ChahatKumar

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.

vcsjones avatar Jun 29 '20 20:06 vcsjones

@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.

Clockwork-Muse avatar Jun 29 '20 22:06 Clockwork-Muse

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

ChahatKumar avatar Jun 29 '20 22:06 ChahatKumar