.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

What is object pooling?

Posted By: Ramesh P     Posted Date: October 19, 2010    Points:2   Category :ASP.Net
In which situation, we will use object pooling?

Responses
Author: laptop charles             
Posted Date: October 19, 2010     Points: 5   

Just refer the following the link.

http://aspalliance.com/1131

Author: laptop charles             
Posted Date: October 19, 2010     Points: 5   

Just refer the following the link.
http://www.dotnetfunda.com/interview/exam387-what-is-object-pooling-.aspx

Author: krishana singh             
Posted Date: October 23, 2010     Points: 5   

Object Pooling:

Object Pooling is something that tries to keep a pool of objects in memory to be re-used later and hence it will reduce the load of object creation to a great extent. This article will try to explain this in detail. The example is for an Employee object, but you can make it general by using Object base class.

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")
}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend