Go to the first, previous, next, last section, table of contents.


Image swap

When you create/load a huge image, the computer memory may not be sufficient to contain all the data. (For example, this may happen if you are working with a scanned image of A4, 720dpi, 24bit fullcolor, even if you have up to 128Mb of memory!) To work with such huge images, CamlImages provides image swaps, which can escape part of the images into files stored on the hard disk. A huge image is thus partitioned into several blocks and if there is not enough free memory, the blocks which have not been accessed recently are swapped to temporary files. If a program requests to access to such a swapped block, the library silently loads it back into memory.

By default, image swapping is disabled, because it slows down the programs. To activate this function, you have to modify Bitmap.maximum_live and Bitmap.maximum_block_size:

  1. Bitmap.maximum_live is the maximum heap live data size of the program (in words)
  2. Bitmap.maximum_block_size is the maximum size of swap blocks (in words).

For example, if you do not want to use more than 10M words (that is 40Mb for a 32bit architecture or 80Mb for a 64bit architecture), set Bitmap.maximum_live to 10000000. You may (and you should) enable heap compaction, look at the GC interface file, gc.mli, in the standard library for more details (you should change the compaction configuration).

Bitmap.maximum_block_size affects the speed and frequency of image block swapping. If it is larger, each swapping becomes slower. If it is smaller, more swappings will occur. Too large and too small maximum_block_size, both may make the program slower. I suggest to have maximum_block_size set to !Bitmap.maximum_live / 10.

If you activated image swapping, cache files for unused swapped blocks will be removed automatically by Caml GC finalization, but you may free them explicitly by hand also. The functions and methods named "destroy" will free those blocks.

The swap files are usually created in the /tmp directory. If you set the environment variable "CAMLIMAGESTMPDIR", then its value replaces the default "/tmp" directory. The temporary files are erased when the program exits successfully. In other situations, for instance in case of spurious exception, you may need to erase temporary files manually.


Go to the first, previous, next, last section, table of contents.