nostra13/Android-Universal-Image-Loader · error · IllegalArgumentException

maxSize <= 0

Error message

maxSize <= 0

What it means

LruMemoryCache is this library's default memory cache (an LRU keyed by URI string with a byte budget). Its constructor requires a strictly positive maxSize because the eviction loop trimToSize(maxSize) needs a real budget to bound the cache; zero or negative sizes would make the cache evict everything forever or never stabilize. The IllegalArgumentException fails fast at configuration time rather than misbehaving later.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/memory/impl/LruMemoryCache.java:33

 * become eligible for garbage collection.<br />
 * <br />
 * <b>NOTE:</b> This cache uses only strong references for stored Bitmaps.
 *
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 * @since 1.8.1
 */
public class LruMemoryCache implements MemoryCache {

	private final LinkedHashMap<String, Bitmap> map;

	private final int maxSize;
	/** Size of this cache in bytes */
	private int size;

	/** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache */
	public LruMemoryCache(int maxSize) {
		if (maxSize <= 0) {
			throw new IllegalArgumentException("maxSize <= 0");
		}
		this.maxSize = maxSize;
		this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
	}

	/**
	 * Returns the Bitmap for {@code key} if it exists in the cache. If a Bitmap was returned, it is moved to the head
	 * of the queue. This returns null if a Bitmap is not cached.
	 */
	@Override
	public final Bitmap get(String key) {
		if (key == null) {
			throw new NullPointerException("key == null");
		}

		synchronized (this) {
			return map.get(key);
		}

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Pass a positive byte size, e.g. memoryCacheSize(16 * 1024 * 1024)
  2. If sizing dynamically, use memoryCacheSizePercentage(availableMemoryPercent) which validates and computes bytes from Runtime.maxMemory()
  3. Guard custom computed sizes: Math.max(1, computedSize)

Example fix

// before
long avail = Runtime.getRuntime().maxMemory();
config.memoryCacheSize((int) (avail * memoryCacheFraction)); // fraction 0.0 -> maxSize <= 0

// after
long avail = Runtime.getRuntime().maxMemory();
int size = (int) (avail * Math.max(0.05f, memoryCacheFraction));
config.memoryCacheSize(Math.max(1, size));
Defensive patterns

Strategy: validation

Validate before calling

int cacheBytes = Math.max(1, computeCacheBytes());
new ImageLoaderConfiguration.Builder(context).memoryCacheSize(cacheBytes);

Try / catch

try {
    builder.memoryCacheSize(size);
} catch (IllegalArgumentException e) {
    builder.memoryCacheSize(16 * 1024 * 1024); // safe default
}

Prevention

When it happens

Trigger: new LruMemoryCache(0), new LruMemoryCache(-1), or ImageLoaderConfiguration.Builder.memoryCacheSize(n) with n <= 0; also computing the size from an expression that underflows or evaluates to 0 (e.g. (int)(maxMemory * 0f)).

Common situations: Deriving cache size from a device-memory heuristic that returns 0 on some devices; passing a resource-dimension value that resolved to 0; copy-pasting a config where memoryCacheSize was meant to be in bytes but a percentage helper returned 0; unit tests constructing the cache with 0.

Related errors


AI-assisted analysis of nostra13/Android-Universal-Image-Loader@ba33ec64d0 (2026-08-14). Data as JSON: /api/errors/79a5cc63ef132650. Report an issue: GitHub.