Click here to Skip to main content
11,645,087 members (64,766 online)
Click here to Skip to main content

C# Object Pooling

, 12 Oct 2007 43.2K 347 35
Rate this:
Please Sign up or sign in to vote.
A simple article on object pooling for the brave

Introduction

Before I get blasted, let me mention Stoyan Damov's article on object pooling. Although my method was developed independently, it does use several of the same techniques.

This code makes use of the WeakReference object to perform object pooling. The assumption is made that if the .NET runtime attempts to reclaim memory before the object is needed again, it is cheaper to reclaim the memory. This assumption is only valid under the assumption I made when designing this class, i.e. this type of object pooling works well for applications that require large number of expensive objects, fast. In fact, the only resource these objects occupy is large sections of memory, of which consecutive sections are expensive to allocate. While I cannot really see an application for this in business settings, primarily because so much of ASP.NET is object creation happy, I can foresee many applications in the scientific realm, especially graphics processing and AI. Although I have not been able to use this code in a professional setting, timings indicate a significant speed improvement over just creating objects over and over.

I am not going to spend a lot of time itemizing the code in this article. Instead it will be included as an attachment. Please read the basic algorithm first (below) and then examine the source. I think this particular project will be more fun to probe and pick apart without direct guidance.

Using the Code

The basic algorithm of the ObjectPool class is as follows:

  • Request a new object via generic method ObjectPool.GetInstance().GetObjectFromPool(null);
  • Loop through the linked list to find an unused object
  • Pass parameters to the object's SetupObject method
  • Return reference
  • On dispose, add to linked list
//#define DISABLE_POOL
using System;
using System.Collections.Generic;
using System.Text;

namespace ObjectPool {
    public interface IObjectPoolMethods : IDisposable {
        /// <span class="code-SummaryComment"><summary>
</span>

The linked list is of a fixed maximum size in this code, however, with the immediate dispose method used by the testing application provided, I rarely see a total number of objects created that exceed the number of threads used. If the runtime has disposed an item in the list, the entire list is truncated since the order of the list is done by last time used. While this isn't actually correct because of the nature of the .NET Garbage Collector, it is effectively correct.

Something that I have noticed with this algorithm/code is that as the memory allocated decreases, the difference between Object Pool and non-object pool goes away (ok, duh) but as the number of objects required in a given unit of time increases, without the pooling, the slower the application runs as the OS begins to thrash. The dispose method does not seem to be called.

Conclusion

Like I said in my brief introduction above, I haven't found a good application for this yet. I might try plugging it into my memory graph function solver algorithm (see my Bridges article for a simple implementation of the solver), but until then it was just a fun proof of concept. I would be interested in comments describing how similar algorithms have been used in a high object count scenario (as opposed to Connection Pools which have low object counts but are resource heavy).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ennis Ray Lynch, Jr.
Architect ERL GLOBAL, INC
United States United States
My company is ERL GLOBAL, INC. I develop Custom Programming solutions for business of all sizes. I also do Android Programming as I find it a refreshing break from the MS.

You may also be interested in...

Comments and Discussions

 
GeneralMax number of objects instanciated Pin
argrithmag21-Feb-08 7:43
memberargrithmag21-Feb-08 7:43 
GeneralRe: Max number of objects instanciated Pin
Ennis Ray Lynch, Jr.21-Feb-08 7:50
memberEnnis Ray Lynch, Jr.21-Feb-08 7:50 
GeneralPooling and Transaction management in .NET Pin
cmsreddy5-Dec-07 2:28
membercmsreddy5-Dec-07 2:28 
GeneralRe: Pooling and Transaction management in .NET Pin
Ennis Ray Lynch, Jr.14-Dec-07 3:16
memberEnnis Ray Lynch, Jr.14-Dec-07 3:16 
QuestionApplications? Pin
Dewey12-Oct-07 17:09
memberDewey12-Oct-07 17:09 
AnswerI was just referring to applications Pin
Ennis Ray Lynch, Jr.12-Oct-07 18:07
memberEnnis Ray Lynch, Jr.12-Oct-07 18:07 
GeneralRe: I was just referring to applications Pin
Mike Doyon16-Oct-07 3:38
memberMike Doyon16-Oct-07 3:38 
GeneralReferring to your sig Pin
Ennis Ray Lynch, Jr.16-Oct-07 5:04
memberEnnis Ray Lynch, Jr.16-Oct-07 5:04 
GeneralYour Signature! Pin
YouMiss18-Nov-07 14:09
memberYouMiss18-Nov-07 14:09 
GeneralRe: Your Signature! Pin
Mike Doyon18-Nov-07 14:16
memberMike Doyon18-Nov-07 14:16 
GeneralRe: I was just referring to applications Pin
everweb18-Nov-08 11:02
membereverweb18-Nov-08 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 12 Oct 2007
Article Copyright 2007 by Ennis Ray Lynch, Jr.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid