{"record":{"id":"413c0e5832dd045f","repo":"nostra13/Android-Universal-Image-Loader","slug":"memorycachesize-must-be-a-positive-number","errorCode":null,"errorMessage":"memoryCacheSize must be a positive number","messagePattern":"memoryCacheSize must be a positive number","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/core/ImageLoaderConfiguration.java","lineNumber":359,"sourceCode":"\t\tpublic Builder tasksProcessingOrder(QueueProcessingType tasksProcessingType) {\n\t\t\tif (taskExecutor != null || taskExecutorForCachedImages != null) {\n\t\t\t\tL.w(WARNING_OVERLAP_EXECUTOR);\n\t\t\t}\n\n\t\t\tthis.tasksProcessingType = tasksProcessingType;\n\t\t\treturn this;\n\t\t}\n\n\t\t/**\n\t\t * Sets maximum memory cache size for {@link android.graphics.Bitmap bitmaps} (in bytes).<br />\n\t\t * Default value - 1/8 of available app memory.<br />\n\t\t * <b>NOTE:</b> If you use this method then\n\t\t * {@link com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache LruMemoryCache} will be used as\n\t\t * memory cache. You can use {@link #memoryCache(MemoryCache)} method to set your own implementation of\n\t\t * {@link MemoryCache}.\n\t\t */\n\t\tpublic Builder memoryCacheSize(int memoryCacheSize) {\n\t\t\tif (memoryCacheSize <= 0) throw new IllegalArgumentException(\"memoryCacheSize must be a positive number\");\n\n\t\t\tif (memoryCache != null) {\n\t\t\t\tL.w(WARNING_OVERLAP_MEMORY_CACHE);\n\t\t\t}\n\n\t\t\tthis.memoryCacheSize = memoryCacheSize;\n\t\t\treturn this;\n\t\t}\n\n\t\t/**\n\t\t * Sets maximum memory cache size (in percent of available app memory) for {@link android.graphics.Bitmap\n\t\t * bitmaps}.<br />\n\t\t * Default value - 1/8 of available app memory.<br />\n\t\t * <b>NOTE:</b> If you use this method then\n\t\t * {@link com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache LruMemoryCache} will be used as\n\t\t * memory cache. You can use {@link #memoryCache(MemoryCache)} method to set your own implementation of\n\t\t * {@link MemoryCache}.\n\t\t */","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/core/ImageLoaderConfiguration.java#L341-L377","documentation":"ImageLoaderConfiguration.Builder.memoryCacheSize(int) validates that the byte budget for the default LruMemoryCache is strictly positive, throwing IllegalArgumentException otherwise (the check ultimately matches LruMemoryCache's own constructor guard). A non-positive size would make the LRU evict every entry immediately or never stabilize, so it is rejected at configuration time. Using this method also switches the memory cache to LruMemoryCache and warns if you had set a custom memoryCache().","triggerScenarios":"memoryCacheSize(0) or a negative value in the builder; computing the size from an expression that yields 0 on the current device (empty free-memory calculation, integer underflow, a 0 multiplier).","commonSituations":"Device-dependent size math returning 0 on low-RAM devices or emulators; passing a megabyte constant without multiplying by 1024*1024 while testing with value 0; config generated from a remote JSON where the field was absent and defaulted to 0.","solutions":["Pass an explicit positive byte value: memoryCacheSize(32 * 1024 * 1024)","For relative sizing use memoryCacheSizePercentage(...) instead of manual math","Clamp computed sizes: Math.max(1, computed)"],"exampleFix":"// before\nint sizeMb = readRemoteConfig(\"mem_cache_mb\"); // 0 when field missing\nbuilder.memoryCacheSize(sizeMb); // may also be 0 bytes even when non-zero check passes\n\n// after\nint sizeMb = Math.max(8, readRemoteConfig(\"mem_cache_mb\"));\nbuilder.memoryCacheSize(sizeMb * 1024 * 1024);","handlingStrategy":"validation","validationCode":"int bytes = Math.max(1, computedCacheBytes);\nbuilder.memoryCacheSize(bytes);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp every dynamically computed cache size to a positive minimum","Prefer memoryCacheSizePercentage(pct) for relative sizing","Always express sizes in bytes with explicit 1024*1024 factors"],"tags":["configuration","memory-cache","validation"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}