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

maxSize <= 0

Error message

maxSize <= 0

What it means

DiskLruCache.open() throws IllegalArgumentException when maxSize is zero or negative. maxSize caps the total bytes the cache may store, so a non-positive cap is meaningless. LruDiskCache avoids this by mapping its '0 = unlimited' convention to Long.MAX_VALUE before calling open().

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/DiskLruCache.java:206

		this.valueCount = valueCount;
		this.maxSize = maxSize;
		this.maxFileCount = maxFileCount;
	}

	/**
	 * Opens the cache in {@code directory}, creating a cache if none exists
	 * there.
	 *
	 * @param directory a writable directory
	 * @param valueCount the number of values per cache entry. Must be positive.
	 * @param maxSize the maximum number of bytes this cache should use to store
	 * @param maxFileCount the maximum file count this cache should store
	 * @throws IOException if reading or writing the cache directory fails
	 */
	public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize, int maxFileCount)
			throws IOException {
		if (maxSize <= 0) {
			throw new IllegalArgumentException("maxSize <= 0");
		}
		if (maxFileCount <= 0) {
			throw new IllegalArgumentException("maxFileCount <= 0");
		}
		if (valueCount <= 0) {
			throw new IllegalArgumentException("valueCount <= 0");
		}

		// If a bkp file exists, use it instead.
		File backupFile = new File(directory, JOURNAL_FILE_BACKUP);
		if (backupFile.exists()) {
			File journalFile = new File(directory, JOURNAL_FILE);
			// If journal file also exists just delete backup file.
			if (journalFile.exists()) {
				backupFile.delete();
			} else {
				renameTo(backupFile, journalFile, false);
			}

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Pass a strictly positive byte limit, e.g. 50 * 1024 * 1024.
  2. If you want an unlimited cache, pass Long.MAX_VALUE (this is exactly what LruDiskCache does for cacheMaxSize == 0).
  3. Prefer the higher-level LruDiskCache or ImageLoaderConfiguration.diskCacheSize(...) which normalize 0 to unlimited for you.

Example fix

// before
DiskLruCache.open(dir, appVersion, 1, 0, 10000); // IllegalArgumentException

// after
DiskLruCache.open(dir, appVersion, 1, Long.MAX_VALUE, 10000);
Defensive patterns

Strategy: validation

Validate before calling

long effectiveMax = (maxSize > 0) ? maxSize : Long.MAX_VALUE; // normalize 0/unset -> unlimited
DiskLruCache.open(dir, appVersion, valueCount, effectiveMax, maxFileCount);

Prevention

When it happens

Trigger: Calling DiskLruCache.open(directory, appVersion, valueCount, 0, maxFileCount) or with a negative maxSize. Also reachable indirectly if a wrapper passes an unnormalized 0 where 0 was intended to mean 'unlimited'.

Common situations: Treating 0 as 'unlimited' when using the ext DiskLruCache directly (that convention belongs to LruDiskCache/DiskCacheConfig, not to open()); computing maxSize from a config value or disk-free-space query that came back 0 or -1; arithmetic that subtracts a reserve from available space and underflows.

Related errors


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