apache/hadoop · error · HadoopIllegalArgumentException
Illegal initial capacity: " + initCapacity
Error message
Illegal initial capacity: " + initCapacity
What it means
LightWeightResizableGSet is a chained, resizable hash set used by NameNode components. Its constructor follows the java.util.HashMap contract: the first argument, initCapacity, is the initial table-size hint, and a negative value throws HadoopIllegalArgumentException("Illegal initial capacity: N") before any LinkedElement[] array is allocated.
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightResizableGSet.java:68
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/** Size of the entry table. */
private int capacity;
/**
* The load factor for the hash set.
*/
private final float loadFactor;
private int threshold;
public LightWeightResizableGSet(int initCapacity, float loadFactor) {
if (initCapacity < 0) {
throw new HadoopIllegalArgumentException("Illegal initial capacity: " +
initCapacity);
}
if (loadFactor <= 0 || loadFactor > 1.0f) {
throw new HadoopIllegalArgumentException("Illegal load factor: " +
loadFactor);
}
this.capacity = actualArrayLength(initCapacity);
this.hash_mask = capacity - 1;
this.loadFactor = loadFactor;
this.threshold = (int) (capacity * loadFactor);
entries = new LinkedElement[capacity];
}
public LightWeightResizableGSet() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
View on GitHub (pinned to 2add963021)
Solutions
- Pass initCapacity >= 0 (0 is legal and means the minimum table)
- Clamp computed values: Math.max(0, expectedSize - slack)
- Use the no-arg constructor (DEFAULT_INITIAL_CAPACITY / DEFAULT_LOAD_FACTOR) when the size is unknown
Example fix
// before new LightWeightResizableGSet(estimated - RESERVED, 0.75f); // after new LightWeightResizableGSet(Math.max(0, estimated - RESERVED), 0.75f);
Defensive patterns
Strategy: validation
Validate before calling
int cap = Math.max(0, estimatedSize - RESERVED); new LightWeightResizableGSet<>(cap, 0.75f);
Try / catch
try { set = new LightWeightResizableGSet<>(cap, 0.75f); } catch (HadoopIllegalArgumentException e) { throw new IllegalArgumentException("Bad GSet init capacity " + cap + ": " + e.getMessage(), e); } Prevention
- Clamp any computed capacity with Math.max(0, ...) at the call site
- Use the no-arg constructor when the size hint is unknown
- Keep a unit test that feeds 0 and negative estimates through your sizing code
When it happens
Trigger: new LightWeightResizableGSet(-1, 0.75f); an initCapacity computed from arithmetic that underflows below zero (e.g. expectedSize - slack); tests enumerating invalid constructor arguments.
Common situations: Deriving the initial table size from a configuration value or size estimate and passing it straight through; copying HashMap-style code where the identical validation exists; a 'not set' -1 sentinel from config reaching the constructor.
Related errors
- Memory " + maxMemory + " must be greater than or equal to 0
- Illegal load factor: " + loadFactor
- {} parameters are required but {} arguments are provided
- Illegal option {}
- Not enough arguments: expected {} but got {}
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/5adfd14940c4d45a.
Report an issue: GitHub.