What is the Object Pool Design Pattern?

Göksu Deniz
16 min readJan 10, 2023

--

Image is created by DALL-E 2 AI

In software engineering, the object pool design pattern is a software creational design pattern that uses a set of initialized objects kept ready to use — a “pool” — rather than creating new objects on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be more efficient than destroying and recreating objects. The object pool pattern can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instantiations in use at any one time is low.

The object pool design pattern is a software design pattern that is used to improve the performance of a program by reducing the cost of creating new objects.

Here’s how it works:

  1. A “pool” of objects is created. These objects are pre-initialized and ready to use.
  2. When an object is needed, it is taken from the pool.
  3. The object is used and then returned to the pool when it is no longer needed.

By using this pattern, the program avoids the overhead of creating new objects each time they are needed. This can be more efficient because creating a new object can take time and resources.

The object pool pattern is useful in situations where:

  • The cost of initializing a class instance is high.
  • The rate of instantiation of a class is high.
  • The number of instantiations in use at any one time is low.

Let’s say you are building a video game that has non-player characters (NPCs) that the player can interact with. Each NPC has a set of behaviors and characteristics that are defined by a “NPC class” in your game’s code.

When the player first enters an area of the game where NPCs are present, your game needs to create new NPC objects for each NPC that the player will encounter. Creating a new NPC object takes a little bit of time, so you want to make sure that your game runs smoothly.

To make NPC creation more efficient, you can use the object pool design pattern. Here’s how it would work: instead of creating a new NPC object every time the player enters an NPC area, your game has a “pool” of pre-created NPC objects that are ready to use. When the player enters an NPC area, your game takes an NPC object from the pool and initializes it with the behaviors and characteristics for the specific NPC that the player will interact with. When the player is finished interacting with the NPC and leaves the area, the NPC object is returned to the pool instead of being deleted.

This way, your game doesn’t have to spend time creating new NPC objects every time the player enters an NPC area, which makes it run more smoothly.

Here is an example of how you might implement the object pool pattern for NPC objects in a C# program:

using System;
using System.Collections.Generic;

// NPC class representing a non-player character in the game
class NPC
{
// NPC behavior and characteristics go here
public void DoSomething()
{
// NPC behavior implementation goes here
}
}

// Object pool for NPC objects
class NPCPool
{
// List of pre-initialized NPC objects
private List<NPC> _npcs;

// Initialize the NPC pool with a specified number of NPC objects
public NPCPool(int size)
{
_npcs = new List<NPC>(size);
for (int i = 0; i < size; i++)
{
_npcs.Add(new NPC());
}
}

// Get an NPC object from the pool
public NPC GetNPC()
{
if (_npcs.Count > 0)
{
NPC npc = _npcs[0];
_npcs.RemoveAt(0);
return npc;
}
else
{
// In this example, we just create a new NPC object if the pool is empty.
// You could also throw an exception or return a null value here.
return new NPC();
}
}

// Return an NPC object to the pool
public void ReturnNPC(NPC npc)
{
_npcs.Add(npc);
}
}

// Example usage of the NPC pool
class Program
{
static void Main(string[] args)
{
// Create an NPC pool with 10 NPC objects
NPCPool npcPool = new NPCPool(10);

// Get an NPC object from the pool and do something with it
NPC npc = npcPool.GetNPC();
npc.DoSomething();

// Return the NPC object to the pool
npcPool.ReturnNPC(npc);
}
}
  • The NPC class represents an NPC object in the game. In the example, it has a single method called DoSomething which represents the behavior of the NPC. In a real-world implementation, this class could have properties and methods that define the appearance, behavior and characteristics of the NPC.
  • The NPCPool class is the object pool for NPC objects. It has a list of pre-initialized NPC objects and provides methods for getting and returning NPC objects to the pool.
  • The constructor NPCPool(int size) creates the NPC pool with a specified number of NPC objects. It creates the _npcs list and adds size number of NPC objects to the list.
  • The GetNPC() method takes an NPC object from the pool and returns it. If the pool is empty it would create a new object.
  • The ReturnNPC(NPC npc) method returns an NPC object to the pool.

In the Main method, an NPC pool is created with 10 NPC objects. Then the GetNPC method of the pool is called to get an NPC object. This NPC object is then used by calling DoSomething() method of the NPC. Once the NPC is done, the ReturnNPC(npc) method is called to return the NPC back to the pool.

When using the object pool design pattern, creating the objects beforehand and recycling those objects when they are not needed, eliminates the overhead of creating new object and makes the process of object creation more efficient. This is what the NPC pool is doing here. Instead of creating a new NPC object every time the player enters an NPC area, the game uses a pre-created NPC objects from the pool and this makes the game run more smoothly.

Here is another example to use object pool design pattern as generic:

using System;
using System.Collections.Generic;

// Object pool class
class ObjectPool<T>
{
// List of pre-initialized objects
private List<T> _objects;

// Initialize the object pool with a specified number of objects
public ObjectPool(int size)
{
_objects = new List<T>(size);
for (int i = 0; i < size; i++)
{
_objects.Add(Activator.CreateInstance<T>());
}
}

// Get an object from the pool
public T GetObject()
{
if (_objects.Count > 0)
{
T obj = _objects[0];
_objects.RemoveAt(0);
return obj;
}
else
{
// In this example, we just create a new object if the pool is empty.
// You could also throw an exception or return a null value here.
return Activator.CreateInstance<T>();
}
}

// Return an object to the pool
public void ReturnObject(T obj)
{
_objects.Add(obj);
}
}

// Example usage of the object pool
class Program
{
static void Main(string[] args)
{
// Create an object pool for strings with 5 string objects
ObjectPool<string> stringPool = new ObjectPool<string>(5);

// Get a string object from the pool and do something with it
string str = stringPool.GetObject();
Console.WriteLine("String from object pool: " + str);

// Return the string object to the pool
stringPool.ReturnObject(str);
}
}

In this example, the ObjectPool class is a generic class that can be used to create an object pool for any type of object. The object pool has a list of pre-initialized objects and provides methods for getting and returning objects to the pool.

The object pool can be used like this:

  1. Create an instance of the ObjectPool class, passing in the number of objects that should be in the pool.
  2. Call the GetObject method to get an object from the pool.
  3. Use the object as needed.
  4. Call the ReturnObject method to return the object to the pool when it is no longer needed.

There is a built-in System.Buffers.ArrayPool<T> class in the .NET Core framework that can be used to create object pools.

ArrayPool<T> is a general-purpose array pool, providing a way for reusing arrays for a variety of scenarios. The primary benefit of the ArrayPool<T> is to reduce the pressure on the garbage collector by reusing arrays instead of allocating new ones.

You can get an instance of an ArrayPool<T> by calling the static ArrayPool<T>.Shared property. Once you have an instance of the ArrayPool<T>, you can call its Rent method to get an array from the pool, and its Return method to return an array to the pool.

Here is an example of how you might use the ArrayPool<T> class to create an object pool for byte arrays:

using System;
using System.Buffers;

class Example
{
static void Main(string[] args)
{
var arrayPool = ArrayPool<byte>.Shared;

byte[] buffer = arrayPool.Rent(1024);
try
{
// Use the buffer
}
finally
{
arrayPool.Return(buffer);
}
}
}

In this example, the ArrayPool<byte>.Shared property is used to get an instance of the ArrayPool<byte> class. The Rent method is called to get a byte array from the pool with a specified size of 1024 bytes. Then the buffer is used and once it's done, it is returned to the pool by calling Return(buffer).

Keep in mind that when you get an array from the pool, you are only guaranteed that the array’s length is at least the size you requested. The actual length of the array may be greater. And when you return an array to the pool, you should clear the content of the array, to avoid leak of sensitive information or for security reasons.

It’s also worth mentioning that using libraries such as Microsoft.Extensions.ObjectPool can provide an easy-to-use, high-performance object pooling mechanism with a simple and extensible API. It provides a base ObjectPool<T> class that you can use to create your own object pool for any type of object, as well as a DefaultObjectPool<T> class that can be used as a simple default implementation.

Here is an example of how you might use the ObjectPool<T> class to create an object pool for MyType objects:

using Microsoft.Extensions.ObjectPool;

class MyTypePooled : IPooledObject
{
// MyType object
}

class MyTypePool : ObjectPool<MyTypePooled>
{
public MyTypePool(IPooledObjectPolicy<MyTypePooled> policy) : base(policy)
{
}
public MyTypePool() : this(new DefaultPooledObjectPolicy<MyTypePooled>())
{
}

public override MyTypePooled Create()
{
return new MyTypePooled();
}
}

And you can use it like this:

var myTypePool = new MyTypePool();

var obj = myTypePool.Get();
try
{
// Use the obj
}
finally
{
myTypePool.Return(obj);
}

This example shows how to create a custom object pool for MyTypePooled objects, by creating a MyTypePool class that inherits from ObjectPool<MyTypePooled>. The MyTypePool class takes the IPooledObjectPolicy<MyTypePooled> as a parameter and have an implementation of Create() that creates a new object of MyTypePooled. The Get and Return methods works the same way as the ArrayPool example.

It’s a good idea to use an object pool when you have a large number of objects that are expensive to create, are used frequently and returned back to the pool after a short period of time. This is because it reduces the pressure on the garbage collector, increases application performance, and reduces memory fragmentation.

Here’s an example of how you might use the ObjectPool<T> class from the Microsoft.Extensions.ObjectPool library to create an object pool for RabbitMQ.Client.IConnection objects, which are used to connect to a RabbitMQ server:

using Microsoft.Extensions.ObjectPool;
using RabbitMQ.Client;

class RabbitMQConnectionPooled : IPooledObject
{
public IConnection Connection { get; set; }
public void Reset() { }
}

class RabbitMQConnectionPool : ObjectPool<RabbitMQConnectionPooled>
{
private readonly ConnectionFactory _factory;

public RabbitMQConnectionPool(IPooledObjectPolicy<RabbitMQConnectionPooled> policy, ConnectionFactory factory) : base(policy)
{
_factory = factory;
}

public override RabbitMQConnectionPooled Create()
{
var conn = _factory.CreateConnection();
return new RabbitMQConnectionPooled { Connection = conn };
}
}

In this example, the RabbitMQConnectionPool is a custom object pool for IConnection objects. It creates a new instance of the RabbitMQConnectionPooled class, which is a simple wrapper class that holds the IConnection object and implements the IPooledObject interface, which has a single Reset method that can be used to reset the state of the object before returning it to the pool. The Create method uses the passed ConnectionFactory object to create new IConnection object, wraps it in RabbitMQConnectionPooled and returns it.

This way you can use RabbitMQConnectionPool class to get an IConnection object from the pool by calling Get() method, and then use the IConnection object to interact with a RabbitMQ server. When you're done with the IConnection object, you can return it to the pool by calling the Return(conn) method, this way the connection can be reused again.

Here’s an example of how you might use the RabbitMQConnectionPool class to create a connection to a RabbitMQ server and publish a message:

var factory = new ConnectionFactory() { HostName = "localhost" };
var connectionPool = new RabbitMQConnectionPool(new DefaultPooledObjectPolicy<RabbitMQConnectionPooled>(), factory);

using (var conn = connectionPool.Get())
{
using (var channel = conn.Connection.CreateModel())
{
channel.QueueDeclare("myqueue", false, false, false, null);
channel.BasicPublish("", "myqueue", null, Encoding.UTF8.GetBytes("Hello, World!"));
Console.WriteLine("Message sent");
}
}

This example creates an instance of the RabbitMQConnectionPool and a ConnectionFactory object with the server address, then gets an IConnection object from the pool by calling Get() method, creates a channel by calling the CreateModel() method, then declares a queue and publishes a message to it. The IConnection object is returned to the pool when the using block ends.

The size of the pool is an important factor that determines the number of objects in the pool.

When creating an instance of the ObjectPool<T> class, you can specify the maximum number of objects that the pool can hold. This size is usually passed as a parameter to the constructor of the ObjectPool<T> class.

To add the pool size you should add a parameter in the constructor and also update the constructor’s implementation like this below:

class RabbitMQConnectionPool : ObjectPool<RabbitMQConnectionPooled>
{
private readonly ConnectionFactory _factory;

public RabbitMQConnectionPool(IPooledObjectPolicy<RabbitMQConnectionPooled> policy, ConnectionFactory factory, int maxSize) : base(policy, maxSize)
{
_factory = factory;
}

public override RabbitMQConnectionPooled Create()
{
var conn = _factory.CreateConnection();
return new RabbitMQConnectionPooled { Connection = conn };
}
}

And then you can create an instance of RabbitMQConnectionPool and pass the maxSize as parameter, for example:

var connectionPool = new RabbitMQConnectionPool(new DefaultPooledObjectPolicy<RabbitMQConnectionPooled>(), factory, 100);

It will make sure that the pool will only keep up to 100 IConnection objects at any given time, if more objects are needed, new connections will be created, otherwise the returned connections will wait in the pool until they're needed again, and when the pool reaches it's maximum size and a new connection is returned to the pool, the connection will be discarded.

It’s worth noting that when creating an object pool, the maximum size is not always a hard limit, some pool implementation could create new objects if they are requested and the pool is empty, or discard objects if the pool exceeds the maximum size but you don’t want to wait for an object to be returned.

Ultimately, it is up to you to decide on the appropriate maximum size for your object pool based on your specific use case and requirements. You should consider the expected number of concurrent requests, the memory and resource usage, and any other constraints and factors that may affect the performance of your application.

If you don’t specify a maximum size when creating an instance of the ObjectPool<T> class, and the library you are using doesn't have a default size , it will not have a maximum size limit.

It means, that pool will keep creating new objects as they are requested, without any limit of number of objects in the pool, which could lead to an unexpected increase in memory usage and other resources.

In some cases, if the object pool is created but the object is not being used, it may also lead to the objects not being disposed, causing the application to hang on to resources that are no longer needed, which is called a resource leak, and it will have a negative impact on the performance of your application.

That’s why it is important to specify the maximum size when creating an object pool. It allows you to control the number of objects in the pool and ensures that the pool does not grow too large, preventing unexpected increase in memory consumption and other resources. Setting a maximum size also helps you avoid creating unnecessary objects and improves performance of the pool.

It’s always a good practice to set a maximum size limit for the object pool, based on your specific use case and requirements, and pay attention to the library you are using and it’s behavior when not specifying the maximum size.

In that specific example actually you can use the singleton design pattern too.

Using the singleton design pattern to manage a single connection to a RabbitMQ server can make the connection more efficient in some cases.

With the singleton pattern, the connection is created only once and it is shared among all the clients that need to use it. This eliminates the overhead of creating multiple connections, which can be a costly operation, particularly when creating a new connection for each client that needs to use it.

Additionally, using a single connection can also reduce the number of resources used by the application, such as file handles, network sockets and so on. This is because a single connection can be reused across multiple clients, instead of creating a new connection for each client.

However, it’s worth noting that, this also means that all clients will be sharing the same connection and channel, which could lead to contention, especially if clients are publishing or consuming messages at a high rate. This can also cause delays in processing messages, and make the connection more vulnerable to failures, since a single failure could bring down all the clients.

That being said, it can be effective in scenarios where the number of clients is small or the rate of message production and consumption is low, in this case you can create a singleton connection with a static variable and make sure that the connection is thread-safe.

It’s important to evaluate your specific use case and requirements when deciding whether to use a singleton pattern for managing connections to a RabbitMQ server. In some situations, it may be more efficient to use a pool of connections, as I described before.

A database connection pool can be considered as an implementation of the object pool design pattern.

The object pool design pattern is a pattern that is used to reuse and manage a limited number of objects for a specific purpose, which can be useful for managing expensive or scarce resources such as database connections.

A database connection pool is a pool of pre-created and ready-to-use database connections that can be quickly and efficiently handed out to applications as needed. It works by maintaining a pool of open connections that can be reused by different parts of the application, reducing the overhead of repeatedly opening and closing database connections. The pool also ensures that connections are returned to the pool once they are no longer needed, making them available for reuse.

In this way, the database connection pool uses the approach of the object pool design pattern to manage the database connections. The pool functions as a container that holds a limited number of open connections, which can be reused by the application, rather than creating a new connection each time one is needed.

It’s worth noting that not all database management systems use the same way for implementing the pooling and the specific implementation will depend on the specific database management system (DBMS) and the driver or library you are using to connect to the database. Some DBMS and driver have a built-in pooling mechanism, others require you to use a third-party library or manually implement the pooling.

Disadvantages

There are a few potential disadvantages of using the Object Pool Design pattern:

  1. Overhead of maintaining the pool: Creating and maintaining a pool of objects can add additional overhead to your application, particularly if the pool is large or the objects in the pool are expensive to create.
  2. Limited flexibility: If the objects in the pool have a limited lifetime or require special handling, the pool may not be able to properly manage those objects.
  3. Synchronization issues: If the pool is accessed by multiple threads simultaneously, proper synchronization must be used to ensure that the pool is used correctly and avoid potential race conditions.
  4. Over allocation: If the maximum size of the pool is not properly defined, it can lead to over allocation of resources and poor performance of the application.
  5. Incorrect object pool configuration: If the object pool is not properly configured, it may not provide the performance benefits that it is intended to provide.

Despite these disadvantages, object pool design pattern is generally considered an efficient approach for managing resources when there are many instances of the same object, and the cost of creating new instances is high.

It’s worth noting that the specific disadvantages may vary depending on the specific implementation of the object pool pattern and the underlying library you are using, but most of the solutions can handle them easily and provide a robust solution. It’s always a good idea to consider carefully the use case and requirements of your application when choosing a design pattern and evaluating the trade-offs.

Conclusion

In conclusion, the Object Pool Design pattern is a design pattern that can be used to improve the performance and memory usage of an application by recycling objects that are expensive to create. The ObjectPool<T> class from the Microsoft.Extensions.ObjectPool library is an implementation of this pattern and allows you to create a pool of objects that can be reused multiple times instead of creating a new instance of the object each time it’s needed. It’s important to specify a maximum size when creating an object pool, because it allows you to control the number of objects in the pool and ensures that the pool does not grow too large and consume too many resources. Also, by specifying the max size, you can avoid creating unnecessary objects and improve the performance of the pool.

There are different ways to implement the object pool pattern. The ObjectPool<T> class from the Microsoft.Extensions.ObjectPool library is just one way to implement the object pool pattern, it's a widely used class provided by the .Net Framework.

Other ways to implement the object pool pattern includes creating a custom class that implements the object pool pattern, or using a third-party library that provides an implementation of the object pool pattern. The important thing is the overall approach of creating and reusing objects and not the specific implementation, using an ObjectPool<T> is just one way to achieve this approach.

The choice of the way to implement the object pool pattern will depend on your specific use case and requirements. It is important to evaluate different options and choose the one that best fits your needs, whether it’s a custom implementation, a third-party library, or the ObjectPool<T> class from the Microsoft.Extensions.ObjectPool library.

It’s always a good practice to evaluate your specific use case and requirements, when deciding whether to use the Object Pool Design pattern and what will be the appropriate size of the pool.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# Reference

It was created with the help of ChatGPT AI.

--

--

Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.