Comments on: Memory Optimization With ArrayPool in C# https://code-maze.com/csharp-arraypool-memory-optimization/ Learn. Code. Succeed. Fri, 22 Mar 2024 11:22:42 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Konstantinos Polychronopoulos https://code-maze.com/csharp-arraypool-memory-optimization/#comment-8687 Sun, 08 Oct 2023 06:53:49 +0000 https://code-maze.com/?p=96167#comment-8687 In reply to Mohamed Magdi.

Hello Mohamed, thanks for your kind words and questions. I will try to answer all of them:

  1. The ArrayPool is initiated at the start of your application and lives there in memory throughout the lifetime of your application.
  2. The location of the ArrayPool depends mainly on your specific use case. Generally, it’s recommended to use a Singleton pattern to create and manage the ArrayPool instance. This ensures that you have a single, shared pool for the entire application that is accessed from anywhere it is called.
  3. The ArrayPool is thread-safe. The purpose of it is to recycle the arrays it has created. So, continuing from answer (2), it is generally recommended to be used across requests.
]]>
By: Mohamed Magdi https://code-maze.com/csharp-arraypool-memory-optimization/#comment-8684 Thu, 05 Oct 2023 09:19:50 +0000 https://code-maze.com/?p=96167#comment-8684 Thanks for the helpful article and fulfilled details,

Have some questions plz:
1- How long does ArrayPool live in memory?
2- Where it should be located, Singleton/Scoped/Static Class?
3- Is it shared between Users/Threads, so many users could use the same pool, or each user should create its own on every request?

]]>
By: Konstantinos Polychronopoulos https://code-maze.com/csharp-arraypool-memory-optimization/#comment-8676 Sun, 01 Oct 2023 09:28:06 +0000 https://code-maze.com/?p=96167#comment-8676 In reply to Andy Eastham.

Hello Andy!
The Array Pool can also be used effectively with arrays of non-primitive types. However, the degree of performance improvement depends on factors like array size, object complexity, and usage patterns. If you frequently allocate and deallocate arrays of the same size, the Array Pool can help reduce memory allocation overhead, even for non-primitive types.

For example, below you may find the benchmark results using a sample class:

public class SampleClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Description { get; set; }
    public DateTime Timestamp { get; set; }
}
|     Method |  ArraySize |              Mean |   Allocated |
|---------------| ------------- |-------------------:|---------------:|
|  ArrayPool |          100 |          30.21 ns |                - |
|  NewArray |          100 |          80.24 ns |        824 B |
|  ArrayPool |        1000 |          29.15 ns |                - |
|  NewArray |        1000 |        750.49 ns |      8024 B |
|  ArrayPool |      10000 |          31.35 ns |                - |
|  NewArray |      10000 |     6,463.10 ns |     80024 B |
|  ArrayPool |    100000 |          36.99 ns |                - |
|  NewArray |    100000 |  115,776.05 ns |  800052 B |
]]>
By: Andy Eastham https://code-maze.com/csharp-arraypool-memory-optimization/#comment-8673 Wed, 27 Sep 2023 23:36:45 +0000 https://code-maze.com/?p=96167#comment-8673 How does this perform with arrays of non primitive values or variable length objects, eg arrays oh jpeg images?

]]>