Object Pooling in C#

.Net 6 Features

Object pooling is a software development design pattern and a container of objects that holds a list of other objects. Once an object is taken from the pool, it is not available in the pool until it is put back. If this is starting to confuse you, consider visiting the TechRepublic Academy! There are dozens of classes on C# from beginner to expert! Object pooling keeps track of Objects — those that are currently in use, the number of objects the pool holds, and whether this number should be increased. Objects in the pool have a lifecycle of consisting of creation, validation, and destroying.

.NET Object Pooling

When building a .NET application, developers may come across objects that are quite expensive to create. In most scenarios, the cost of creating new objects is high enough to impact application performance. The object pool design pattern is quite helpful in such scenarios. Keeping reusable instances of objects in a resource pool – and using them as needed – boosts the performance of .NET applications. When an application requests an object to use, if the object is available from the pool, it is returned from the pool. In case the object requested by the application is not available from the pool, a new instance of the object is created and returned to the source program.

Read: Code Refactoring Best Practices

Creating an Object Pool in C#

To demonstrate object pooling, I will be using a factory pattern. I will create a factory method, which will take care of creating the objects. During a request for a new object, the factory method will look into the object pool, which is a queue of objects. If there is any object available within the allowed limit, it will return the object; otherwise, a new object will be created and returned back.

The following .NET code example will explain object creation, adding an object into the queue, and returning it from the queue.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;


namespace PrjObjectPooling
{
   class Program
   {
      static void Main(string[] args)
      {
         Factory fa = new Factory();
         Student myStu = fa.GetStudent();
         Console.WriteLine("First object");
         Student myStu1 = fa.GetStudent();
         Console.WriteLine("Second object");
         Student myStu2 = fa.GetStudent();
         Console.WriteLine("Third object");
         Console.Read();
      }
   }


   class Factory
   {
      // Maximum objects allowed!
      private static int _PoolMaxSize = 3;
      // My Collection Pool
      private static readonly Queue objPool = new
         Queue(_PoolMaxSize);
      public Student GetStudent()
      {
         Student oStudent;
         // Check from the collection pool. If exists, return
         // object; else, create new
         if (Student.ObjectCounter >= _PoolMaxSize &&
            objPool.Count > 0)
         {
            // Retrieve from pool
            oStudent = RetrieveFromPool();
         }
         else
         {
            oStudent = GetNewStudent();
         }
         return oStudent;
      }
      private Student GetNewStudent()
      {
         // Creates a new Student
         Student oStu = new Student();
         objPool.Enqueue(oStu);
         return oStu;
      }
      protected Student RetrieveFromPool()
      {
         Student oStu;
         // Check if there are any objects in my collection
         if (objPool.Count > 0)
         {
            oStu = (Student)objPool.Dequeue();
            Student.ObjectCounter--;
         }
         else
         {
            // Return a new object
            oStu = new Student();
         }
         return oStu;
      }
   }
   class Student
   {
      public static int ObjectCounter = 0;
      public Student()
      {
         ++ObjectCounter;
      }
      private string _Firstname;
      private string _Lastname;
      private int _RollNumber;
      private string _Class;


      public string Firstname
      {
         get
         {
            return _Firstname;
         }
         set
         {
            _Firstname = value;
         }
      }

      public string Lastname
      {
         get
         {
            return _Lastname;
         }
         set
         {
            _Lastname = value;
         }
      }

      public string Class
      {
         get
         {
            return _Class;
         }
         set
         {
            _Class = value;
         }
      }

      public int RollNumber
      {
         get
         {
            return _RollNumber;
         }
         set
         {
            _RollNumber = value;
         }
      }
   }
}

Object pooling is very similar to database connection pooling. Remember, the minimum and maximum number of objects that could be added to an object pool are configurable. During design, your pool following strategies could be implemented in the following manner:

  • Return null or throw an exception if the application needs an object from the pool but the maximum number of objects have been allocated.
  • Block the call until an object is available.
  • Increase the pool size to accommodate more objects.

A developer can modify this custom object pool implementation to allow the minimum and maximum sizes of the object pool to be read from a configuration file. As part of the initialization of the object pool, we also can ensure that the pool contains the minimum number of objects in it.

Object pools help to reduce resource overhead when you need multiple instances of a class that is expensive to create or manage. If your application involves instantiating the same classes over and over again, use this design pattern to ensure optimal performance.

Read: A Simple Thread Pooling Approach

Conclusion to .NET Object Pooling

I hope this article has provided you with basic information about object pooling and how you can create a pool in .NET. That’s all for today. Happy reading!

Read more .NET programming tutorials and how-tos.

More by Author

Must Read