Object pool

Object pool

In computer programming, an object pool is a software design pattern. An object pool is a set of initialised objects that are kept ready to use, rather than allocated and destroyed on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished with an object, it returns it to the pool, rather than destroying it. It is a specific type of factory object.

Object pooling can offer a significant performance boost; it is most effective in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low.

Handling of empty pools

Object pools employ one of three strategies to handle a request when there are no spare objects in the pool.

# Fail to provide an object (and return an error to the client).
# Allocate a new object, thus increasing the size of the pool. Pools that do this usually allow you to set the high water mark (the maximum number of objects ever used).
# In a multithreaded environment, a pool may block the client until another thread returns an object to the pool.

Pitfalls

When writing an object pool, the programmer has to be careful to make sure the state of the objects returned to the pool is reset back to a sensible state for the next use of the object. If this is not observed, the object will often be in some state that was unexpected by the client program and may cause the client program to fail. The pool is responsible for resetting the objects, not the clients. Object pools full of objects with dangerously stale state are sometimes called object cesspools and regarded as an anti-pattern.

The presence of stale state is not always an issue; it becomes dangerous when the presence of stale state causes the object to behave differently. For example, an object that represents authentication details may break if the "successfully authenticated" flag is not reset before it is passed out, since it will indicate that a user is correctly authenticated (possibly as someone else) when they haven't yet attempted to authenticate. However, it will work just fine if you fail to reset some value only used for debugging, such as the identity of the last authentication server used.

Inadequate resetting of objects may also cause an information leak. If an object contains confidential data (e.g. a user's credit card numbers) that isn't cleared before the object is passed to a new client, a malicious or buggy client may disclose the data to an unauthorized party.

C# Examples

In C#, there are a few objects that implement this pattern. System.Threading.ThreadPool is configured to have a predefined number of threads to allocate. When the threads are returned, they are available for another computation. Thus, one can use threads without paying the cost of creation and disposal of threads.

References

*cite conference
last = Kircher
first = Michael
coauthors = Prashant Jain;
title = Pooling Pattern
booktitle = EuroPLoP 2002
place = Germany
date = 2002-07-04
url = http://www.kircher-schwanninger.de/michael/publications/Pooling.pdf
accessdate = 2007-06-09

External links

* [http://www.oodesign.com/object-pool-pattern.html OODesign article]
* [http://msdn2.microsoft.com/en-us/library/ms682822.aspx Improving Performance with Object Pooling (Microsoft Developer Network )]
* [http://www.developer.com/java/ent/article.php/626171 Developer.com article]
* [http://c2.com/cgi-bin/wiki?ObjectPoolPattern Portland Pattern Repository entry]
* [http://commons.apache.org/pool/ Apache Commons Pool: A mini-framework to correctly implement object pooling in Java]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Object pool pattern — For the article about a general pool see Pool (Computer science) In computer programming, an object pool is a software design pattern. An object pool is a set of initialised objects that are kept ready to use, rather than allocated and destroyed… …   Wikipedia

  • Pool — may refer to: Bodies of water*Plunge pool, small, deep body of water *The River Pool, river in England, tributary to the River Ravensbourne *Reflecting pool, shallow pool of water designed to reflect a structure and its surroundings: **Capitol… …   Wikipedia

  • Pool (computer science) — A pool in computer science is a set of initialised resources that are kept ready to use, rather than allocated and destroyed on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When… …   Wikipedia

  • pool — pool1 [po͞ol] n. [ME < OE pol, akin to Du poel & Ger pfuhl, prob. ult. < IE base * bhel , to shine, glimmer] 1. a small pond, as in a garden 2. a small collection of liquid, as a puddle 3. SWIMMING POOL 4. a deep, still spot in a river ☆ …   English World dictionary

  • object ball — n. Billiards Pool any ball other than the cue ball …   English World dictionary

  • pool — pool1 /poohl/, n. 1. a small body of standing water; pond. 2. a still, deep place in a stream. 3. any small collection of liquid on a surface: a pool of blood. 4. a puddle. 5. See swimming pool. 6. a subterranean accumulation of oil or gas held… …   Universalium

  • pool — {{Roman}}I.{{/Roman}} noun 1 swimming pool, etc. ADJECTIVE ▪ bathing, paddling (BrE), swimming, wading (AmE) ▪ big ▪ little, small …   Collocations dictionary

  • Pool of Radiance — Infobox VG| title = Pool of Radiance developer = Strategic Simulations, Inc. publisher = Strategic Simulations, Inc. designer = engine = Gold Box released = genre = Role playing game, Tactical RPG modes = Single player ratings = platforms = Amiga …   Wikipedia

  • pool — I. noun Etymology: Middle English, from Old English pōl; akin to Old High German pfuol pool Date: before 12th century 1. a. (1) a small and rather deep body of usually fresh water (2) a quiet place in a stream (3) a body of water forming above a… …   New Collegiate Dictionary

  • pool — I. /pul / (say poohl) noun 1. a small body of standing water; pond. 2. a puddle. 3. any small collection of liquid on a surface: a pool of blood. 4. a still, deep place in a stream. 5. a swimming pool. {Middle English and Old English pōl} II.… …  

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”