{"record":{"id":"940ee5b4db692732","repo":"ruby-concurrency/concurrent-ruby","slug":"required-array-size-too-large-940ee5","errorCode":null,"errorMessage":"Required array size too large","messagePattern":"Required array size too large","errorType":"exception","errorClass":"OutOfMemoryError","httpStatus":null,"severity":"critical","filePath":"ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java","lineNumber":3514,"sourceCode":"         * @return the map backing this view\n         */\n        public ConcurrentHashMapV8<K,V> getMap() { return map; }\n\n        public final int size()                 { return map.size(); }\n        public final boolean isEmpty()          { return map.isEmpty(); }\n        public final void clear()               { map.clear(); }\n\n        // implementations below rely on concrete classes supplying these\n        abstract public Iterator<?> iterator();\n        abstract public boolean contains(Object o);\n        abstract public boolean remove(Object o);\n\n        private static final String oomeMsg = \"Required array size too large\";\n\n        public final Object[] toArray() {\n            long sz = map.mappingCount();\n            if (sz > (long)(MAX_ARRAY_SIZE))\n                throw new OutOfMemoryError(oomeMsg);\n            int n = (int)sz;\n            Object[] r = new Object[n];\n            int i = 0;\n            Iterator<?> it = iterator();\n            while (it.hasNext()) {\n                if (i == n) {\n                    if (n >= MAX_ARRAY_SIZE)\n                        throw new OutOfMemoryError(oomeMsg);\n                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)\n                        n = MAX_ARRAY_SIZE;\n                    else\n                        n += (n >>> 1) + 1;\n                    r = Arrays.copyOf(r, n);\n                }\n                r[i++] = it.next();\n            }\n            return (i == n) ? r : Arrays.copyOf(r, i);\n        }","sourceCodeStart":3496,"sourceCodeEnd":3532,"githubUrl":"https://github.com/ruby-concurrency/concurrent-ruby/blob/0b88d5ff75f69b3740c8f0868e76f833cb2fd45d/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java#L3496-L3532","documentation":"Thrown by the JSR166e ConcurrentHashMapV8 backport that concurrent-ruby bundles under ext/ for JRuby. The map's collection views (keySet/values/entrySet) implement Object[] toArray() by reading map.mappingCount() and refusing to allocate a result array larger than MAX_ARRAY_SIZE (Integer.MAX_VALUE - 8), since JVM arrays cannot exceed that. The pre-check at this line fires when the live mapping count already exceeds the ceiling before any element is copied.","triggerScenarios":"On JRuby, calling .to_a / toArray() on a keySet, values, or entrySet view of a Java-backed ConcurrentHashMapV8 (for example the Java extension used by Concurrent::Map or a jruby-specific cache class) while the map holds more than Integer.MAX_VALUE - 8 (2,147,483,639) mappings.","commonSituations":"In practice this signals runaway map growth, not a transient condition: an unbounded cache or counter map in a long-lived process crossing ~2.1 billion entries, a producer loop inserting without eviction, or a bulk-import bug duplicating keys. Normal applications never approach this threshold.","solutions":["Do not materialize the view: iterate it lazily via its Java iterator (view.iterator) instead of view.to_a.","Fix the unbounded growth: add eviction/expiry or switch to a bounded cache so the map stays far below Integer.MAX_VALUE.","If a snapshot is truly needed, copy in bounded batches (iterate and append into chunks) or move data to an external store.","Profile what inserts into the map - a 2-billion-entry map is almost always a defect (missing dedup, duplicate keys, or a tight insert loop)."],"exampleFix":"# before\nkeys = java_map.keySet.to_a   # OutOfMemoryError: Required array size too large\n\n# after\nit = java_map.keySet.iterator\nwhile it.hasNext\n  process(it.next)\nend","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"begin\n  arr = view.to_a\nrescue Java::JavaLang::OutOfMemoryError => e\n  raise unless e.message == 'Required array size too large'\n  it = view.iterator                 # fall back to lazy iteration\n  while it.hasNext\n    handle(it.next)\n  end\nend","preventionTips":["Never call .to_a on views of unbounded maps - iterate lazily.","Bound every long-lived concurrent map with eviction or capacity.","Monitor map size metrics; treat this error as a growth-defect alarm, not a transient failure."],"tags":["jruby","java","concurrent-hash-map","out-of-memory","array-limits","concurrency"],"backgroundTag":"jvm-array-size-limit-exceeded","analyzedSha":"0b88d5ff75f69b3740c8f0868e76f833cb2fd45d","analyzedAt":"2026-08-21T20:12:56.291Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}