Object pooling is a design pattern where an array of ready-to-use objects is stored. Every time a request is made for a new object, the pool manager will accept it and fulfill it by allocating an object from the pool.
It’s useful when the objects that are being managed are subjected to the following: • Whenever the same classes are instantiated repeatedly. • When allocation or initialization of an object is expensive. • When objects are used regularly and consistently.
Consider the following approaches when implementing your object pool: • If the application needs an object but the maximum number of objects is already allocated in the pool, an exception should be thrown or the app should return null. • Calls will be blocked until the object is available. • To store more objects, the pool can increase its size.
Object pooling has two forms: • An object is pulled from the pool on activation of the object. • An object is added to the pool on deactivation.
• It is similar to database connection pooling. • With the help of object pooling, objects in .NET can be reused repeatedly without having to be created from scratch. • Improves performance when the initialization of an object is expensive.
• There is a restriction on the number of objects that the object pool will keep, not the number of objects it will allocate. • The pool is not de-allocated until all the objects managed by the pool are de-allocated.