Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:09:22+00:00 2026-06-12T14:09:22+00:00

I am working on a pooling algorithm which takes an allocated object and recycles

  • 0

I am working on a pooling algorithm which takes an allocated object and recycles it allocation for later. However, one of the issues of recycling an object is for instance:

 someObject obj = pool.alloc(); //gives me a new object if no previous allocations, If an allocation has been recycled, returns a previous allocation
 obj.someVariable = "foo";
 pool.recycle(obj);

The above code will take an existing allocation and save it so that I don’t have to allocate any extra ram in case I was to have another someObject. However, the following creates an issue:

 someObject obj = pool.alloc(); //gives me the above allocation
 obj.otherVariable = "bar";
 obj.dump();

As a result, I will get the following result:

  someVariable = foo
  otherVariable = bar

The above approach creates an issue. If I for some reason (or someone else) have an algorithm that doesn’t use certain variables inside of an object, the old values can cause unnecessary behavior. I have poked around a little bit to see if there is some way I can call the default constructor again (bad idea) and C# (thankfully) doesn’t seem to allow you to do so. However, I was wondering if there was some way to use reflection to do this? Also, does clearing the variables in an object defeat the purpose of avoiding malloc (new)? In other words, if I spend the time clearing variables does the performance gain become minimal? I am trying to teach myself pooling so any criticism and advice is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:09:24+00:00Added an answer on June 12, 2026 at 2:09 pm

    Here’s one way of looking at pooling:

    Pooling is only really necessary when you are reusing some complicated setup work that you can avoid by retrieving an already existing object. For example, one of the best known applications for object pooling is in the form of “Database Connections”.

    Pooling works for Database Connections (DBConns) because 1) a DBConn can be trivially identified by a connection string; and 2) a DBConn takes a lot of work and time to establish. The trivial identification is done by matching connection strings – if two connection strings are identical, the connections they establish should also be identical. Also, once you have a connection string, it can take hundreds of milliseconds to lookup the server address, open a socket to it, authenticate, and establish the connection. This means that pooling works well for database connections because when a connection is released, it can be reused the next time a connection is requested for the same connection string.

    The .NET runtime environment is exceptionally good at allocating objects quickly and releasing them so you don’t have memory issues. If all you’re worried about is memory use or the speed of allocation, don’t; you can’t beat the compiler’s performance by zeroing out memory on your own. However, if your objects have some complex, lengthy setup that can be avoided by retrieving an existing object from a pool, you can find some benefit.

    Another good example of pooling is a particle system in a videogame. You have hundreds of particles that have to be created, go through a lifecycle, and be destroyed, only for new particles to be created after the old ones die. A typical particle system will create X number of objects as an array, and have a Reset() function that returns a dead object to life as a newly created particle in the original location. The reason this works is again because particles can be trivially identified, and the setup work (giving the graphics system the texture, putting locations, etc).

    Does your application have both a “trivial match identification” capability and a lengthy setup process that can be avoided? If not, just allocate the objects new each time – I would wager you won’t see any performance degradation.

    EDIT: From a performance perspective, let’s look at this here:

    Resetting variables = O(N) assignment statements; using reflection can increase that significantly

    Instantiate a new object = One malloc call, one constructor call; the complexity level is dependent on the constructor code, and memory fragmentation.

    Using reflection to reset variables can work, but you have to know in advance that O(N) for assignments is faster than your malloc and constructor. For a database connection, I know that condition is satisfied; but is that true for your pool?

    EDIT: From your comments below, you may indeed have found a case where pooling is appropriate. If that’s the case, I would suggest the ideal approach is to create a Reset() function for any class that you are pooling. Try creating an IPoolable interface that defines a function Reset(). Then, for each class you pool, define the Reset() function so that it zeroes out all key variables. Because it compiles, it won’t incur reflection overhead, and you can maintain object-specific optimizations that wouldn’t be possible with dynamic code.

    For the pool, define the pool class as MyPool<IPoolable>; then whenever an object is retrieved that was previously recycled, you can call Reset() on it before handing it back to the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an iPhone application which will use long-polling to send event notifications
I am using connection pooling of tomcat with oracle database. It is working fine,
I'm working on a small project which deals with polling devices to check states
Am working on a POC for self learning in which I want to keep
I am working with a database which is very slow. We use ASP.NET to
We're trying to get connection pooling working with uodotnet and currently failing miserably. When
How can I setup connection pooling in Spring MVC? I am working on an
I'm working currently on a long polling script where i've to check a database
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working with an undisclosed API, I found a function that can set the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.