Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Managing Memory

As mentioned earlier, in JAI image data is available in cache as tiles. To manage memory allocation for tiles, JAI provides an interface called TileCache. You can create an instance of TileCache by calling the factor method JAI.getTileCache(). By default, the tile cache size is set to 64MB. To change this setting, the TileCache interface specifies the following set method:

  • public void setMemoryCapacity(long memoryCapacity)

Here's an example that sets the memory:

TileCache tileCache = JAI.getDefaultInstance().getTileCache();

Here's the corresponding get method:

  • public long getMemoryCapacity()

The memoryCapacity parameter is specified in bytes. If the memory capacity is smaller than the current capacity, the tiles in the cache are flushed to achieve the desired settings. If you set a large amount of memory for the tile cache, interactive operations are faster but the tile cache fills up very quickly. If you set a low amount of memory for the tile cache, the performance degrades. So there's a trade-off between memory and speed.

The TileCache interface also has a method for setting the capacity as a specific number of tiles:

  • public void setTileCapacity(int tileCapacity)

and a corresponding get method:

  • public int getTileCapacity()

In JAI 1.0.2, implementing these methods does nothing. In JAI 1.1, these methods are deprecated.

The other utility methods are

  • public void add(java.awt.image.RenderedImage owner,
                                       int tileX,
                                       int tileY,
                                       Raster data)
  • public void remove(java.awt.image.RenderedImage owner,
                                               int tileX,
                                               int tileY)
  • public Raster getTile(RenderedImage owner,
                                                 int tileX,
                                                 int tileY)
  • public void removeTiles(RenderedImage owner)

  • public void flush()

In these methods the tileX and tileY parameters are the indices of the tile in the planar image. The flush() method discards all the tiles in the cache, and the removeTiles() method discards only tiles belonging to the image passed as the input parameter.

TileCache is also a rendering hint, and it can also be set through JAI's setTileCache() method.

  • + Share This
  • 🔖 Save To Your Account