Object pooling : Java Glossary
home O words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
Object pooling
If you create large numbers of identical short-lived objects, you may be able to allocate them faster using a manual method, rather than doing it with the standard new and gc. There are three main dangers to recycling objects:
  1. You must manually free each object when you are done with it. If you fail to do so, you will have a memory leak. You are back to C++ manual alloc and free.
  2. It is totally up to you to ensure there are no links to an object you are about to recycle.
  3. Constructors will not automatically be run to initialise objects freshly. You must handle such code yourself, outside the constructor.
For a very simple, quick implementation, you simply put your no-longer needed objects on a unidirectional recycle chain and when you need one, instead of using new, you just allocate the head of the chain, and remove the element from the chain. If you start getting too many empties on your recycled chain, you just chop the excess tail end of the chain off allow them to be garbage collected. You don’t have to free them individually. If you run out of objects, use new to create some more. This technique is very powerful in C++ but does not work as well in Java because Java has more efficient allocation techniques than C++.


CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.17] The information on this page is for non-military use only.
You are visitor number 11,364. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/objectpooling.html J:\mindprod\jgloss\objectpooling.html