stanfordnlp/CoreNLP · error · RuntimeException

You cannot set capacity to smaller than the current size.

Error message

You cannot set capacity to smaller than the current size.

What it means

ArrayCoreMap.setCapacity resizes the internal key/value arrays but refuses to shrink below the current number of stored annotations, since that would silently drop data. It throws this RuntimeException when the requested new capacity is smaller than size.

Solutions

  1. Query size() first and clamp: call setCapacity(Math.max(requested, map.size())).
  2. If shrinking is truly needed, remove annotations until size() <= newSize, then setCapacity.
  3. Only call setCapacity for growth (preallocation), not reduction.
  4. Wrap in a check-and-skip when unsure of current fill level.

Example fix

// before
map.setCapacity(4); // throws if map.size() == 10
// after
if (map.size() <= 4) { map.setCapacity(4); } else { /* remove keys first */ }
Defensive patterns

Strategy: validation

Validate before calling

int requested = 4;
if (requested >= coreMap.size()) coreMap.setCapacity(requested);

Try / catch

try { map.setCapacity(n); } catch (RuntimeException e) { if (e.getMessage().contains("smaller than the current size")) { map.setCapacity(map.size()); } else { throw e; } }

Prevention

When it happens

Trigger: Calling setCapacity(newSize) with newSize < the map's current size (number of annotations stored), e.g. after heavily populating a CoreMap then trying to trim its capacity.

Common situations: Manual memory-tuning code that guesses a capacity without querying size(); reusing a pooled/populated ArrayCoreMap and shrinking capacity; copying a capacity constant from an empty map onto a filled one.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/af54aa9725d4f8ac. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/ArrayCoreMap.java:265


  /**
   * Reduces memory consumption to the minimum for representing the values
   * currently stored stored in this object.
   */
  public void compact() {
    if (keys.length > size) {
      Class[] newKeys = new Class[size];
      Object[] newValues = new Object[size];
      System.arraycopy(keys, 0, newKeys, 0, size);
      System.arraycopy(values, 0, newValues, 0, size);
      keys = ErasureUtils.uncheckedCast(newKeys);
      values = newValues;
    }
  }

  public void setCapacity(int newSize) {
    if (size > newSize) { throw new RuntimeException("You cannot set capacity to smaller than the current size."); }
    Class[] newKeys = new Class[newSize];
    Object[] newValues = new Object[newSize];
    System.arraycopy(keys, 0, newKeys, 0, size);
    System.arraycopy(values, 0, newValues, 0, size);
    keys = ErasureUtils.uncheckedCast(newKeys);
    values = newValues;
  }

  /**
   * Returns the number of elements in this map.
   * @return The number of elements in this map.
   */
  @Override
  public int size() {
    return size;
  }

  /**

View on GitHub (pinned to 1b7edd19c4)