didi/DoKit · error · IllegalArgumentException
Max size must be positive.
Error message
Max size must be positive.
What it means
LruCache's constructor throws IllegalArgumentException when maxSize is <= 0. The cache needs a positive byte budget to evict against; a zero or negative budget is a configuration mistake. Fail-fast guard, consistent with android.util.LruCache.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/LruCache.java:43
public class LruCache implements Cache {
final LinkedHashMap<String, Bitmap> map;
private final int maxSize;
private int size;
private int putCount;
private int evictionCount;
private int hitCount;
private int missCount;
/** Create a cache using an appropriate portion of the available RAM as the maximum size. */
public LruCache(Context context) {
this(Utils.calculateMemoryCacheSize(context));
}
/** Create a cache with a given maximum size in bytes. */
public LruCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("Max size must be positive.");
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
}
@Override public Bitmap get(String key) {
if (key == null) {
throw new NullPointerException("key == null");
}
Bitmap mapValue;
synchronized (this) {
mapValue = map.get(key);
if (mapValue != null) {
hitCount++;
return mapValue;
}
missCount++;View on GitHub (pinned to 626827cddb)
Solutions
- Clamp the computed size to a sensible positive minimum before constructing: Math.max(size, 256 * 1024)
- Use the LruCache(Context) constructor, which sizes the cache from available RAM automatically
- Check the size-computation math for truncation or overflow
Example fix
// before int size = (int) (Runtime.getRuntime().maxMemory() / 10f * 0); // oops -> 0 new LruCache(size); // IllegalArgumentException // after int size = Math.max((int) (Runtime.getRuntime().maxMemory() / 10), 1024 * 1024); new LruCache(size);
Defensive patterns
Strategy: validation
Validate before calling
int size = Math.max(computeCacheBytes(context), 1024 * 1024); new LruCache(size); // or simply new LruCache(context)
Type guard
boolean isValidCacheSize(int maxSize) { return maxSize > 0; } Prevention
- Clamp computed cache sizes to a positive minimum
- Prefer the Context constructor, which derives a valid size from device RAM
When it happens
Trigger: Constructing new LruCache(0) or new LruCache(negative), e.g. computing a percentage of free memory that rounded/truncated to zero.
Common situations: Cache size computed from device metrics (free RAM, heap percentage) that yields 0 on constrained devices or emulators; hardcoded 0 during testing; integer overflow when computing a large size that wraps negative.
Related errors
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/cde26f8a546417bf.
Report an issue: GitHub.