Class ResourcePool



  • public class ResourcePool
    extends Object
    This object manages a pool of resources (arrays and images) for frequent repetitive reuse.

    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:

    • A limit of the exact resource type. For example: by default there will only be 5 images of the same width, height and type.
    • A limit of all resources of a given type. For example: by default there will only be 20 images total.

    (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.

    See Also:
    ResourcePoolDemo
    • Nested Class Summary

      Nested Classes

      Modifier and Type Class and Description
      static class  ResourcePool.Limit
      The limit for each type of resource this pool manages.
    • Constructor Summary

      Constructors

      Constructor and Description
      ResourcePool()
      Create a new ResourcePool.
    • Method Summary

      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.Limit getLimit(com.bric.util.ResourcePool.Type type)
      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 bi)
      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.
    • Constructor Detail

      • ResourcePool

        public ResourcePool()
        Create a new ResourcePool.
    • Method Detail

      • get

        public static ResourcePool get()
        Return the default ResourcePool.
      • clean

        protected void clean()
        Iterate over all resources and make sure nothing has expired based on the current time limits and time stamps.
      • getLimit

        protected ResourcePool.Limit getLimit(com.bric.util.ResourcePool.Type type)
        Return the Limit for a given resource type.
      • getImage

        public BufferedImage getImage(int width,
                                      int height,
                                      int type,
                                      boolean clear)
        Return a BufferedImage from this pool. If no cached images exists matching these parameters: then a new image is created.
        Parameters:
        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).
        Returns:
        an image matching the arguments provided. If possible this will recycle a previously cached image.
      • finalize

        protected void finalize()
      • getIntArray

        public int[] getIntArray(int length)
        Return an int array from this pool. If no cached int array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • getFloatArray

        public float[] getFloatArray(int length)
        Return a float array from this pool. If no cached float array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • getDoubleArray

        public double[] getDoubleArray(int length)
        Return a double array from this pool. If no cached double array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • getShortArray

        public short[] getShortArray(int length)
        Return a short array from this pool. If no cached short array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • getLongArray

        public long[] getLongArray(int length)
        Return a long array from this pool. If no cached long array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • getByteArray

        public byte[] getByteArray(int length)
        Return a byte array from this pool. If no cached byte array exists matching the requested length: then a new array is created.
        Parameters:
        length - the array length
      • put

        public boolean put(long[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(float[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(int[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(byte[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(short[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(double[] array)
        Store an array in this pool for future reuse.
        Returns:
        true if the array was stored, false if this pool is not currently accepting any more arrays of this type/size.
      • put

        public boolean put(BufferedImage bi)
        Store an image in this pool for future reuse.
        Returns:
        true if the image was stored, false if this pool is not currently accepting any more images of this type/size.