memoObject Pooling In .NET

Object Pooling

Object Pooling in .NET allows objects to be kept 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 Employee object 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 does Object Pooling work?

We are going to use the Factory pattern for this purpose. We will have a factory method, which will take care of 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 given 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.

using System;
using System.Collections;

namespace ObjectPooling
{
    class Factory
    {
        // Maximum objects allowed!
        private static int _PoolMaxSize = 2;
        // My Collection Pool
        private static readonly Queue objPool = new Queue(_PoolMaxSize);

        public Employee GetEmployee()
        {
            Employee oEmployee;
            // check from the collection pool. If exists return object else create new
            if (Employee.Counter >= _PoolMaxSize && objPool.Count > 0)
            {
                // Retrieve from pool
                oEmployee = RetrieveFromPool();
            }
            else
            {
                oEmployee = GetNewEmployee();
            }
            return oEmployee;
        }

        private Employee GetNewEmployee()
        {
            // Creates a new employee
            Employee oEmp = new Employee();
            objPool.Enqueue(oEmp);
            return oEmp;
        }

        protected Employee RetrieveFromPool()
        {
            Employee oEmp;
            // if there is any objects in my collection
            if (objPool.Count > 0)
            {
                oEmp = (Employee)objPool.Dequeue();
                Employee.Counter--;
            }
            else
            {
                // return a new object
                oEmp = new Employee();
            }
            return oEmp;
        }
    }

    class Employee
    {
        public static int Counter = 0;
        public Employee()
        {
            ++Counter;
        }
        private string _Firstname;
        public string Firstname
        {
            get
            {
                return _Firstname;
            }
            set
            {
                _Firstname = value;
            }
        }
    }
}

Code 2. How to use it?

private void button1_Click(object sender, EventArgs e)
{
    Factory fa = new Factory();
    Employee myEmp = fa.GetEmployee();
    Console.WriteLine("First object");
    Employee myEmp1 = fa.GetEmployee();
    Console.WriteLine("Second object");
}


Recommended Free Ebook
Similar Articles