Class TileCache

All Implemented Interfaces:
ConcurrentMap<TileCache.Key,Raster>, Map<TileCache.Key,Raster>

final class TileCache extends Cache<TileCache.Key,Raster>
A cache of tiles computed by ComputedImage. A common cache is shared by all images. Tiles are kept by strong references until a memory usage limit is reached, in which case the references of oldest tiles become soft references.

Design note

The use of a common cache for all images makes easier to set an application-wide limit (for example 25% of available memory). The use of soft reference does not cause as much memory retention as it may seem because those references are hold only as long as the image exist. When an image is garbage collected, the corresponding soft references are cleaned.
Since:
1.1
Version:
1.3
  • Field Details

    • GLOBAL

      static final TileCache GLOBAL
      The instance shared by all image operations. Current version does not allow to specify a custom cache for some images, but a future version may allow that.
  • Constructor Details

    • TileCache

      private TileCache()
      Creates a new tile cache. We put an arbitrary limit of 25% of available memory. If more tiles are created, some strong references will become soft references. Because strong references may be kept by the JVM, the amount of memory actually used may be greater than this limit. However, those references are cleaned when the image owning those tiles is garbage collected.
  • Method Details

    • cost

      protected int cost(Raster tile)
      Returns an estimation of the memory consumption of the given tile.
      Overrides:
      cost in class Cache<TileCache.Key,Raster>
      Parameters:
      tile - the tile for which to estimate memory usage.
      Returns:
      memory used by the given tile, in bytes.
      See Also:
    • flush

      public boolean flush()
      Forces the removal of all garbage collected tiles. This method should not need to be invoked. It is provided as a debugging tools when suspecting a memory leak.
      Overrides:
      flush in class Cache<TileCache.Key,Raster>
      Returns:
      true if some entries have been removed as a result of this method call.
      See Also: