memoObject Pooling In .NET

Object Pooling in .NET allows objects to keep in the memory pool so the objects can be reused without recreating them. This article explains what object pooling is in .NET and how to implement object pooling in C#.

Object Pooling

 
Object Pooling in .NET allows objects to keep in the memory pool so the objects can be reused without recreating them. This article explains what object pooling is in .NET and how to implement object pooling in C#. In this code example, we have an Employee class. We're going to create an Emplyee object and keep it in the object pool and use if from there. 
 

What does Object Pooling mean?

 
Object Pool is a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.
 

How Object Pooling works?

 
We are going to use the Factory pattern for this purpose. We will have a factory method, which will take care about the creation of objects. Whenever there is a request for a new object, the factory method will look into the object pool (we use Queue object). If there is any object available within the allowed limit, it will return the object (value object), otherwise a new object will be created and give you back.
 
The below code is just an example to give you an idea, and is neither tested nor error-proof. You can modify it as you wish: up-to your creativity.
 
Code 1
 
Object Pool and Employee class.
  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ObjectPooling  
  5. {  
  6.     class Factory  
  7.     {  
  8.         // Maximum objects allowed!  
  9.         private static int _PoolMaxSize = 2;  
  10.         // My Collection Pool  
  11.         private static readonly Queue objPool = new Queue(_PoolMaxSize);    
  12.         public Employee GetEmployee()  
  13.         {  
  14.             Employee oEmployee;  
  15.             // check from the collection pool. If exists return object else create new  
  16.             if (Employee.Counter >= _PoolMaxSize && objPool.Count>0)  
  17.             {  
  18.                 // Retrieve from pool  
  19.                 oEmployee = RetrieveFromPool();  
  20.             }  
  21.             else  
  22.             {  
  23.                 oEmployee = GetNewEmployee();  
  24.             }  
  25.             return oEmployee;  
  26.         }  
  27.         private Employee GetNewEmployee()  
  28.         {  
  29.             // Creates a new employee  
  30.             Employee oEmp = new Employee();  
  31.             objPool.Enqueue(oEmp);  
  32.             return oEmp;  
  33.         }  
  34.         protected Employee RetrieveFromPool()  
  35.         {  
  36.             Employee oEmp;  
  37.             // if there is any objects in my collection  
  38.             if (objPool.Count>0)  
  39.             {  
  40.                 oEmp = (Employee)objPool.Dequeue();  
  41.                 Employee.Counter--;  
  42.             }  
  43.             else  
  44.             {  
  45.                 // return a new object  
  46.                 oEmp = new Employee();  
  47.             }  
  48.             return oEmp;  
  49.         }  
  50.     }  
  51.     class Employee  
  52.     {  
  53.         public static int Counter = 0;  
  54.         public Employee()  
  55.         {  
  56.             ++Counter;  
  57.         }  
  58.         private string _Firstname;  
  59.         public string Firstname  
  60.         {  
  61.             get  
  62.             {  
  63.                 return _Firstname;  
  64.             }  
  65.             set  
  66.             {  
  67.                 _Firstname = value;  
  68.             }  
  69.         }   
  70.     }  
  71. }  
Code 2
 
How to use it?
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Factory fa = new Factory();  
  4.     Employee myEmp = fa.GetEmployee();  
  5.     Console.WriteLine("First object");  
  6.     Employee myEmp1 = fa.GetEmployee();  
  7.     Console.WriteLine("Second object");  
  8. }