{"record":{"id":"af54aa9725d4f8ac","repo":"stanfordnlp/CoreNLP","slug":"you-cannot-set-capacity-to-smaller-than-the-curren","errorCode":null,"errorMessage":"You cannot set capacity to smaller than the current size.","messagePattern":"You cannot set capacity to smaller than the current size\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/ArrayCoreMap.java","lineNumber":265,"sourceCode":"\n\n  /**\n   * Reduces memory consumption to the minimum for representing the values\n   * currently stored stored in this object.\n   */\n  public void compact() {\n    if (keys.length > size) {\n      Class[] newKeys = new Class[size];\n      Object[] newValues = new Object[size];\n      System.arraycopy(keys, 0, newKeys, 0, size);\n      System.arraycopy(values, 0, newValues, 0, size);\n      keys = ErasureUtils.uncheckedCast(newKeys);\n      values = newValues;\n    }\n  }\n\n  public void setCapacity(int newSize) {\n    if (size > newSize) { throw new RuntimeException(\"You cannot set capacity to smaller than the current size.\"); }\n    Class[] newKeys = new Class[newSize];\n    Object[] newValues = new Object[newSize];\n    System.arraycopy(keys, 0, newKeys, 0, size);\n    System.arraycopy(values, 0, newValues, 0, size);\n    keys = ErasureUtils.uncheckedCast(newKeys);\n    values = newValues;\n  }\n\n  /**\n   * Returns the number of elements in this map.\n   * @return The number of elements in this map.\n   */\n  @Override\n  public int size() {\n    return size;\n  }\n\n  /**","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/ArrayCoreMap.java#L247-L283","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Query size() first and clamp: call setCapacity(Math.max(requested, map.size())).","If shrinking is truly needed, remove annotations until size() <= newSize, then setCapacity.","Only call setCapacity for growth (preallocation), not reduction.","Wrap in a check-and-skip when unsure of current fill level."],"exampleFix":"// before\nmap.setCapacity(4); // throws if map.size() == 10\n// after\nif (map.size() <= 4) { map.setCapacity(4); } else { /* remove keys first */ }","handlingStrategy":"validation","validationCode":"int requested = 4;\nif (requested >= coreMap.size()) coreMap.setCapacity(requested);","typeGuard":null,"tryCatchPattern":"try { map.setCapacity(n); } catch (RuntimeException e) { if (e.getMessage().contains(\"smaller than the current size\")) { map.setCapacity(map.size()); } else { throw e; } }","preventionTips":["Only use setCapacity for preallocation/growth.","Clamp requested capacity to at least size().","Track how many annotations you store before sizing pooled CoreMaps."],"tags":["capacity","coremap","invalid-state","precondition"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}