Package com.day.cq.dam.commons.util
Class MemoryUtil
- java.lang.Object
-
- com.day.cq.dam.commons.util.MemoryUtil
-
public class MemoryUtil extends java.lang.Object
This class offers some useful memory management functions for asset/image processing
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MemoryUtil.Callback<T>
Callback throwing IOException and returning some (image) object fortryUntilEnoughMemory(com.day.cq.dam.api.Asset, int, com.day.cq.dam.commons.util.MemoryUtil.Callback)
-
Field Summary
Fields Modifier and Type Field Description static long
MIN_CQ_MEMORY_REQUIREMENT
We assume about 100 megs as minimal cq requirement...
-
Constructor Summary
Constructors Constructor Description MemoryUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
getExpectedImageMemory(Asset asset)
Calculates the expected image memory consumption.static boolean
hasEnoughMemory(Asset asset)
Checks if enough memory is available for the asset/image processing Note: it works for images only!static boolean
hasEnoughSystemMemory(Asset asset)
Checks if the available max.static long
suggestMaxHeapSize(Asset asset)
Suggests the minimal max.static <T> T
tryUntilEnoughMemory(Asset asset, int maxTrials, MemoryUtil.Callback<T> callback)
Tries to execute the callback until no memory exception occurs anymore.static <T> T
tryUntilEnoughMemory(Asset asset, MemoryUtil.Callback<T> callback)
Tries to execute the callback until no memory exception occurs anymore.
-
-
-
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 fromcallback
- 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 frommaxTrials
- maximum number of trials, must be > 0callback
- 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) otherwisefalse
-
getExpectedImageMemory
public static long getExpectedImageMemory(Asset asset)
Calculates the expected image memory consumption. Bytes required by each pixel are calculated from metadata fields likephotoshop:ColorMode
ortiff: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) otherwisefalse
-
suggestMaxHeapSize
public static long suggestMaxHeapSize(Asset asset)
Suggests the minimal max. heap size- Parameters:
asset
- asset to check- Returns:
- minimal max. heap size
-
-