{"record":{"id":"5b4e5b6ea84c1183","repo":"ruby-concurrency/concurrent-ruby","slug":"required-array-size-too-large","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/ConcurrentHashMapV8.java","lineNumber":3522,"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":3504,"sourceCodeEnd":3540,"githubUrl":"https://github.com/ruby-concurrency/concurrent-ruby/blob/0b88d5ff75f69b3740c8f0868e76f833cb2fd45d/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java#L3504-L3540","documentation":"CHMView.toArray() is the shared implementation behind keySet().toArray(), values().toArray() and entrySet().toArray(). It sizes the result from map.mappingCount() and throws OutOfMemoryError(\"Required array size too large\") up front when the count exceeds MAX_ARRAY_SIZE (Integer.MAX_VALUE - 8), because no Java array can hold more references. It is a fail-fast size guard, not a report of actual heap exhaustion.","triggerScenarios":"map.keySet().toArray(), map.values().toArray(), map.entrySet().toArray(), or constructors that copy collections such as new ArrayList<>(map.keySet()) when map.mappingCount() exceeds Integer.MAX_VALUE - 8.","commonSituations":"Multi-billion-entry caches or counters on very large 64-bit heaps; passing views to helper APIs that materialize arrays; load and stress tests with huge key spaces.","solutions":["Check map.mappingCount() <= Integer.MAX_VALUE - 8 before calling toArray(); if over, do not materialize","Stream instead of snapshot: view.forEach(k -> ...) processes elements without a backing array","Batch with the iterator into fixed-size chunks when arrays are genuinely needed","If you truly need more than 2^31 elements, shard across multiple maps — no single Java array can hold them"],"exampleFix":"// before\nObject[] keys = map.keySet().toArray();  // OutOfMemoryError: Required array size too large\n\n// after\nif (map.mappingCount() <= Integer.MAX_VALUE - 8L) {\n    Object[] keys = map.keySet().toArray();\n} else {\n    map.keySet().forEach(k -> consume(k)); // never materialize\n}","handlingStrategy":"validation","validationCode":"static final long MAX_ARRAY = Integer.MAX_VALUE - 8L;\nif (map.mappingCount() > MAX_ARRAY) {\n    map.keySet().forEach(k -> consume(k)); // stream, no array\n} else {\n    Object[] snapshot = map.keySet().toArray();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check mappingCount() before any toArray on views of huge maps","Prefer forEach/iterator over materializing arrays for very large maps","Wrap third-party calls that copy Collections with a size pre-check","Remember Integer.MAX_VALUE - 8 is the hard array ceiling"],"tags":["java","out-of-memory","array","concurrency","concurrent-ruby"],"backgroundTag":"required-array-size-too-large","analyzedSha":"0b88d5ff75f69b3740c8f0868e76f833cb2fd45d","analyzedAt":"2026-08-21T20:12:56.291Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}