public class ResourcePool extends Object
Sometimes constantly reallocating resources in tight loops can be an expensive drain, so this should help relieve that performance expense.
This pool also has limitations, though, so it does not introduce a memory leak. Strong references to resources are purged after a fixed time interval (usually 5 seconds). Also there are limits to the number of resources of any given type that will be stored. There are two such limits:
(Arrays have a higher tolerance.)
It is still possible to abuse this model and introduce memory problems, in the same way that is possible to run out of memory just by constructing a few 10,000x10,000 images. But when used responsibly, this should be a valuable tool to easy the cost of constructing thousands of similar objects.
Every time you retrieve an object from this pool, you should wrap the following code in a try/finally block and return the object back to this pool when finished.
ResourcePoolDemo
| Modifier and Type | Class and Description |
|---|---|
static class |
ResourcePool
The limit for each type of resource this pool manages.
|
| Constructor and Description |
|---|
ResourcePool()
Create a new
ResourcePool.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
clean()
Iterate over all resources and make sure nothing has expired based on the current time limits and time stamps.
|
protected void |
finalize()
|
static ResourcePool |
get()
Return the default
ResourcePool.
|
byte[] |
getByteArray(int length)
Return a byte array from this pool.
|
double[] |
getDoubleArray(int length)
Return a double array from this pool.
|
float[] |
getFloatArray(int length)
Return a float array from this pool.
|
BufferedImage |
getImage(int width, int height, int type, boolean clear)
Return a BufferedImage from this pool.
|
int[] |
getIntArray(int length)
Return an int array from this pool.
|
protected ResourcePool |
getLimit(com
Return the Limit for a given resource type.
|
long[] |
getLongArray(int length)
Return a long array from this pool.
|
short[] |
getShortArray(int length)
Return a short array from this pool.
|
boolean |
put(BufferedImage
Store an image in this pool for future reuse.
|
boolean |
put(byte[] array)
Store an array in this pool for future reuse.
|
boolean |
put(double[] array)
Store an array in this pool for future reuse.
|
boolean |
put(float[] array)
Store an array in this pool for future reuse.
|
boolean |
put(int[] array)
Store an array in this pool for future reuse.
|
boolean |
put(long[] array)
Store an array in this pool for future reuse.
|
boolean |
put(short[] array)
Store an array in this pool for future reuse.
|
public static ResourcePoolget()
ResourcePool.
protected void clean()
protected ResourcePool.Limit getLimit(com .bric .util .ResourcePool .Type type)
public BufferedImagegetImage(int width, int height, int type, boolean clear)
width - the width of the image.
height - the height of the image.
type - the type of the image.
clear - if true and a cached image is identified: then that image is first cleared (using an AlphaComposite.Clear).
protected void finalize()
public int[] getIntArray(int length)
length - the array length
public float[] getFloatArray(int length)
length - the array length
public double[] getDoubleArray(int length)
length - the array length
public short[] getShortArray(int length)
length - the array length
public long[] getLongArray(int length)
length - the array length
public byte[] getByteArray(int length)
length - the array length
public boolean put(long[] array)
public boolean put(float[] array)
public boolean put(int[] array)
public boolean put(byte[] array)
public boolean put(short[] array)
public boolean put(double[] array)
public boolean put(BufferedImagebi)