{"record":{"id":"79a5cc63ef132650","repo":"nostra13/Android-Universal-Image-Loader","slug":"maxsize-0-79a5cc","errorCode":null,"errorMessage":"maxSize <= 0","messagePattern":"maxSize <= 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/cache/memory/impl/LruMemoryCache.java","lineNumber":33,"sourceCode":" * become eligible for garbage collection.<br />\n * <br />\n * <b>NOTE:</b> This cache uses only strong references for stored Bitmaps.\n *\n * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)\n * @since 1.8.1\n */\npublic class LruMemoryCache implements MemoryCache {\n\n\tprivate final LinkedHashMap<String, Bitmap> map;\n\n\tprivate final int maxSize;\n\t/** Size of this cache in bytes */\n\tprivate int size;\n\n\t/** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache */\n\tpublic LruMemoryCache(int maxSize) {\n\t\tif (maxSize <= 0) {\n\t\t\tthrow new IllegalArgumentException(\"maxSize <= 0\");\n\t\t}\n\t\tthis.maxSize = maxSize;\n\t\tthis.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);\n\t}\n\n\t/**\n\t * Returns the Bitmap for {@code key} if it exists in the cache. If a Bitmap was returned, it is moved to the head\n\t * of the queue. This returns null if a Bitmap is not cached.\n\t */\n\t@Override\n\tpublic final Bitmap get(String key) {\n\t\tif (key == null) {\n\t\t\tthrow new NullPointerException(\"key == null\");\n\t\t}\n\n\t\tsynchronized (this) {\n\t\t\treturn map.get(key);\n\t\t}","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/memory/impl/LruMemoryCache.java#L15-L51","documentation":"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.","triggerScenarios":"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)).","commonSituations":"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.","solutions":["Pass a positive byte size, e.g. memoryCacheSize(16 * 1024 * 1024)","If sizing dynamically, use memoryCacheSizePercentage(availableMemoryPercent) which validates and computes bytes from Runtime.maxMemory()","Guard custom computed sizes: Math.max(1, computedSize)"],"exampleFix":"// before\nlong avail = Runtime.getRuntime().maxMemory();\nconfig.memoryCacheSize((int) (avail * memoryCacheFraction)); // fraction 0.0 -> maxSize <= 0\n\n// after\nlong avail = Runtime.getRuntime().maxMemory();\nint size = (int) (avail * Math.max(0.05f, memoryCacheFraction));\nconfig.memoryCacheSize(Math.max(1, size));","handlingStrategy":"validation","validationCode":"int cacheBytes = Math.max(1, computeCacheBytes());\nnew ImageLoaderConfiguration.Builder(context).memoryCacheSize(cacheBytes);","typeGuard":null,"tryCatchPattern":"try {\n    builder.memoryCacheSize(size);\n} catch (IllegalArgumentException e) {\n    builder.memoryCacheSize(16 * 1024 * 1024); // safe default\n}","preventionTips":["Never pass a computed size without clamping to >= 1","Prefer memoryCacheSizePercentage(pct) over manual byte math","Unit-test configuration building with 0/negative inputs"],"tags":["memory-cache","configuration","validation","lru"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}