Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Serial Number in SSRS Articles
How to Print a Crystal Report direct to printer Articles
Get Started Developing for Android Apps with Eclipse Articles
Razor View Engine Interview Questions Answers Interview Questions
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
SPIRE PDF Library Articles
Infosys Interview Questions Interview Questions
Html5 interview questions and answers Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
SharePoint 2010 interview Questions Answers Interview Questions
Submit Articles | More Articles..

Object Pooling with .Net

Posted By: sunilbanare On:5/16/2012 11:44:26 AM in:Articles Category:.NET Framework Hits:2747
Object Pooling is a concept that implies that we can store a pool of objects in memory to be reused later and, hence, reduce the load of object creation to a great extent. An Object Pool, also known as a Resource Pool, is a list/set of ready to be used reusable objects that reduce the overhead of creating each object from the scratch whenever a request for an object creation comes in. 

Introduction

Object Pooling is a concept that implies that we can store a pool of objects in memory to be reused later and, hence, reduce the load of object creation to a great extent. An Object Pool, also known as a Resource Pool, is a list/set of ready to be used reusable objects that reduce the overhead of creating each object from the scratch whenever a request for an object creation comes in.

 

An Object Pool is a container of objects that are ready for use. Lists of ready-to-be-used objects are contained in this pool. Whenever a new request for an object creation comes in, the request is served by allocating an object from the pool. Therefore, it reduces the overhead of creating and re-creating objects each time an object creation is required. "An object pool is an object that holds a list of other objects, ready to make them available for use (to yet another object, probably). It does the management work involved, like keeping track of which objects are currently in use, how many objects the pool holds, whether this number should be increased."

 

This whitepaper throws light on this concept (Object Pooling) and discusses how we can implement a simple generic Object Pool in .NET.

 

 

 


 

Advantages of Object Pooling

The biggest advantage of using Object Pooling is that it minimizes the consumption of memory and the system's resources by recycling and re­using objects as and when it is needed and serving the request for new objects from the pool of ready-to-be-used objects. The objects that the application no longer need are sent back to the pool rather than destroying them from the memory. Once an application is up and running, memory utilization is affected by the number and size of objects the system requires. Object pooling reduces the number of allocations, and therefore the number of garbage collections, required by an application. Pooling is quite simple: an object is reused instead of allowing it to be reclaimed by the garbage collector. Objects are stored in some type of list or array called the pool, and handed out to the client on request. This is especially useful when an instance of an object is repeatedly used, or if the object has an expensive initialization aspect to its construction such that it's better to reuse an existing instance than to dispose of an existing one and to create a completely new one from scratch.


 

 

Object Pooling and performance

Object pooling can be extremely effective in certain circumstances, yielding substantial increases in performance. To reuse objects to the best advantage, you should pool as many resources as possible, distinguishing initialization from actual work performed, and then administratively tailor the pool characteristics to actual hardware at deployment time.

 

This involves: Writing the object to factor out expensive initialization and resource acquisition that is performed for any client as a prerequisite to doing actual work on the client’s behalf. You should write heavy object constructors to pool as many resources as possible, so that these are held by the object and immediately available when clients get an object from the pool.

 

Administratively configuring the pool to achieve the best balance in available hardware resources, usually trading the memory dedicated to maintain a pool of a certain size in exchange for faster client access and use of objects. At a certain point, pooling will achieve diminishing returns, and you can get good enough performance while limiting possible resource usage by a particular component.

 

 


 

Implementing Object pooling with .Net

 

We would design an object pool based on some predefined goals/objectives. These are stated as under the following:

Ø       Ease of use and reusable

Ø       Thread Safe

Ø       Type Safe

Ø       Scalable

Ø       Configurable

 



These are the basic objectives that the pool should adhere to. With these in mind, we will now implement a simple Object Pool in C# and use this pool for creation, usage and destruction of objects.

 

 

The Pool Manager Class

The following example illustrates how an object pool can be created. I have provided enough comments to enable the reader to understand how the Pool Manager class works. This class is based on the Singleton Pattern, i.e., there can be at any point of time only one instance of this class.

 

Listing 1

using System;

using System.ComponentModel;

using System.Collections;

using System.Threading;

namespace ObjectPooling

{

    /// <summary>     

    /// A class to manage objects in a pool.

    ///The class is sealed to prevent further inheritence     

    /// and is based on the Singleton Design.     

    /// </summary>     

    public sealed class PoolManager

    {

        private Queue poolQueue = new Queue();

        private Hashtable objPool = new Hashtable();

        private static readonly object objLock = new object();

        private const int POOL_SIZE = 10;

        private int objCount = 0;

        private static PoolManager poolInstance = null;

 

        /// <summary>           

        /// Private constructor to prevent instantiation           

        /// </summary>           

        private PoolManager() { }

 

        /// <summary>           

        /// Static constructor that gets called only once during the application's lifetime.           

        /// </summary>           

        static PoolManager()

        {

            poolInstance = new PoolManager();

        }

 

        /// <summary>           

        /// Static property to retrieve the instance of the Pool Manager           

        /// </summary>

        public static PoolManager Instance

        {

            get

            {

                if (poolInstance != null)

                {

                    return poolInstance;

                }

                return null;

            }

        }

 

        /// <summary>           

        /// Creates objects and adds them in the pool           

        /// </summary>           

        /// <param name="obj">The object type</param>

        public void CreateObjects(object obj)

        {

            object _obj = obj;

            objCount = 0;

            poolQueue.Clear();

            objPool.Clear();

            for (int objCtr = 0; objCtr < POOL_SIZE; objCtr++)

            {

                _obj = new object();

                lock (objLock)

                {

                    objPool.Add(_obj.GetHashCode(), _obj); poolQueue.Enqueue(_obj); objCount++;

                }

            }

        }

 

        /// <summary>

        /// Adds an object to the pool           

        /// </summary>           

        /// <param name="obj">Object to be added</param>           

        /// <returns>True if success, false otherwise</returns>           

        public bool AddObject(object obj)

        {

            if (objCount == POOL_SIZE) return false;

            lock (objLock)

            {

                objPool.Add(obj.GetHashCode(), obj); poolQueue.Enqueue(obj);

                objCount++;

            }

            return true;

        }

 

        /// <summary>           

        /// Releases an object from the pool at the end of the queue           

        /// </summary>           

        /// <returns>The object if success, null otherwise</returns>

        public object ReleaseObject()

        {

            if (objCount == 0) return null;

            lock (objLock)

            {

                objPool.Remove(poolQueue.Dequeue().GetHashCode());

                objCount--;

                if (poolQueue.Count > 0) return poolQueue.Dequeue();

            }

            return null;

        }

 

        /// <summary>           

        /// Releases an object from the pool           

        /// </summary>            

        /// <param name="obj">Object to remove from the pool</param>

        /// <returns>The object if success, null otherwise</returns>           

        public object ReleaseObject(object obj)

        {

            if (objCount == 0) return null;

            lock (objLock)

            {

                objPool.Remove(obj.GetHashCode());

                objCount--;

                RePopulate();

                return obj;

            }

        }

 

        /// <summary>           

        /// Method that repopulates the Queue after an object has been removed from the pool.          

        /// This is done to make the queue objects in sync with the objects in the hash table.          

        /// </summary>           

        private void RePopulate()

        {

            if (poolQueue.Count > 0) poolQueue.Clear();

            foreach (int key in objPool.Keys)

            {

                poolQueue.Enqueue(objPool[key]);

            }

        }

 

        /// <summary>          

        /// Property that represents the current no of objects in the pool          

        /// </summary>           

        public int CurrentObjectsInPool

        {

            get

            {

                return objCount;

            }

        }

 

        /// <summary>            

        /// Property that represents the maximum no of objects in the pool           

        /// </summary>           

        public int MaxObjectsInPool

        {

            get

            {

                return POOL_SIZE;

            }

        }

    }

}

 

 

Using the Pool Manager Class

The following code in Listings 2, 3 and 4 show how we can use the PoolManager created in Listing 1.

 

Listing 2

 

object obj1 = new object();

object obj2 = new object();

PoolManager poolManager = PoolManager.Instance;

poolManager.AddObject(obj1); poolManager.AddObject(obj2);

MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());

poolManager.ReleaseObject(obj1);

MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());

object obj = null;

for (; ; )

{

    obj = poolManager.ReleaseObject();

    if (obj != null)

        MessageBox.Show(obj.GetHashCode().ToString());

    else

    {

        MessageBox.Show("No more objects in the pool"); break;

    }

}

 

 

Listing 3

 

PoolManager poolManager = PoolManager.Instance;

object obj1 = new object();

object obj2 = new object();

object obj3 = new object();

poolManager.AddObject(obj1);

poolManager.AddObject(obj2);

MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());

object obj = poolManager.ReleaseObject();

MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());

if (obj.GetHashCode() == obj1.GetHashCode())

    MessageBox.Show("Object 1 has been released from the pool");

else if (obj.GetHashCode() == obj2.GetHashCode())

    MessageBox.Show("Object 2 has been released from the pool");

 

 

Listing 4

 

PoolManager poolManager = PoolManager.Instance;

ArrayList arr = new ArrayList();

poolManager.CreateObjects(arr);

object obj = poolManager.ReleaseObject();

MessageBox.Show("No of objects in Pool is: " + poolManager.CurrentObjectsInPool.ToString(), "The hash code of the released object is: " + obj.GetHashCode().ToString());

obj = poolManager.ReleaseObject();

MessageBox.Show("No of objects in Pool is: " + poolManager.CurrentObjectsInPool.ToString(), "The hash code of the released object is: " + obj.GetHashCode().ToString());

 

 

 

Note how we have used the PoolManager in the code listings above. The CreateObjects method has been used in Listing IV to create a specified number of objects of the ArrayList type and store them in the pool. However, the major drawback of this design is that there would be enough boxing and un-boxing overhead involved on storing and retrieving the objects to and fro from the pool. To eliminate this, I would recommend the usage of Generics. Further, the size of the pool (the maximum number of objects that the pool can contain) is also fixed and is not configurable. I leave it to the readers to make the necessary changes to the Pool Manager class to suit their needs.

 


 

Summary

Object Pooling is a mechanism that facilitates re-using of the existing objects in a pool (a container of live objects) rather than creating or re­creating them anew each time they are needed. This article has discussed the basic concepts of Object Pooling and how we can implement the same in .NET using C# as the language of choice. Stay tuned for more such articles on Object Pooling where I will discuss more on real-life implementation issues related to this concept. I hope readers will find this article helpful in giving a basic understanding of Object Pooling and how it works apart from implementing a simple Object Pool in C#.

comments powered by Disqus
User Profile
sunilbanare
IT Manager
NA , NA
Email :You must Log In to access the contact details.
Latest Post from :sunilbanare
CDS – Restructuring
View: 2610 | Submitted on: 5/16/2012 11:45:58 AM
Object Pooling with .Net
View: 2747 | Submitted on: 5/16/2012 11:44:26 AM
Cross and Outer Apply in SQL Server
View: 2924 | Submitted on: 5/16/2012 11:43:06 AM
Submit Articles | All Post of This User..


Advertise About Us Private Policy Terms of use
All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher