Class MemoryUtil


  • public class MemoryUtil
    extends java.lang.Object
    This class offers some useful memory management functions for asset/image processing
    • Field Detail

      • MIN_CQ_MEMORY_REQUIREMENT

        public static long MIN_CQ_MEMORY_REQUIREMENT
        We assume about 100 megs as minimal cq requirement...
    • Constructor Detail

      • MemoryUtil

        public MemoryUtil()
    • Method Detail

      • tryUntilEnoughMemory

        public static <T> T tryUntilEnoughMemory​(Asset asset,
                                                 MemoryUtil.Callback<T> callback)
        Tries to execute the callback until no memory exception occurs anymore. Will first check if the asset can be loaded with the given JVM memory settings at all, then go into a loop catching "not enough memory" exceptions and retry until loading worked or a maximum number of retries is reached (100). If it fails, null will be returned.
        Type Parameters:
        T - return type (image object etc.)
        Parameters:
        asset - asset to load Java 2D image from
        callback - method that actually loads an image involving Java 2D memory, return value will be passed through and returned by this method
        Returns:
        the value returned by the callback if successful, or null if memory limits prevented loading within a certain number of trials
      • tryUntilEnoughMemory

        public static <T> T tryUntilEnoughMemory​(Asset asset,
                                                 int maxTrials,
                                                 MemoryUtil.Callback<T> callback)
        Tries to execute the callback until no memory exception occurs anymore. Will first check if the asset can be loaded with the given JVM memory settings at all, then go into a loop catching "not enough memory" exceptions and retry until loading worked or the given maximum number of retries is reached. If it fails, null will be returned.
        Type Parameters:
        T - return type (image object etc.)
        Parameters:
        asset - asset to load Java 2D image from
        maxTrials - maximum number of trials, must be > 0
        callback - method that actually loads an image involving Java 2D memory, return value will be passed through and returned by this method
        Returns:
        the value returned by the callback if successful, or null if memory limits prevented loading within a certain number of trials
      • hasEnoughSystemMemory

        public static boolean hasEnoughSystemMemory​(Asset asset)
        Checks if the available max. memory is enough to process the image
        Parameters:
        asset - asset to check
        Returns:
        true either if the check succeeds or the check cannot be performed (no image data available e.g. the asset is not an image) otherwise false
      • getExpectedImageMemory

        public static long getExpectedImageMemory​(Asset asset)
        Calculates the expected image memory consumption. Bytes required by each pixel are calculated from metadata fields like photoshop:ColorMode or tiff:SamplesPerPixel. If this information can be derived from metadata then it is defaulted to 4.
        Parameters:
        asset - asset to check
        Returns:
        the calculated amount or -1 in case the expected memory cannot be calculated
      • hasEnoughMemory

        public static boolean hasEnoughMemory​(Asset asset)
        Checks if enough memory is available for the asset/image processing Note: it works for images only!
        Parameters:
        asset - asset to check
        Returns:
        true either if the check succeeds or the check cannot be performed (no image data available e.g. the asset is not an image) otherwise false
      • suggestMaxHeapSize

        public static long suggestMaxHeapSize​(Asset asset)
        Suggests the minimal max. heap size
        Parameters:
        asset - asset to check
        Returns:
        minimal max. heap size