{"record":{"id":"f507417eedba3f80","repo":"apache/hadoop","slug":"memory-maxmemory-must-be-greater-than-or-e","errorCode":null,"errorMessage":"Memory \" + maxMemory + \" must be greater than or equal to 0","messagePattern":"Memory \" \\+ maxMemory \\+ \" must be greater than or equal to 0","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":388,"sourceCode":"   * @param mapName mapName.\n   * @param percentage percentage.\n   * @return compute capacity.\n   */\n  public static int computeCapacity(double percentage, String mapName) {\n    return computeCapacity(Runtime.getRuntime().maxMemory(), percentage,\n        mapName);\n  }\n  \n  @VisibleForTesting\n  static int computeCapacity(long maxMemory, double percentage,\n      String mapName) {\n    if (percentage > 100.0 || percentage < 0.0) {\n      throw new HadoopIllegalArgumentException(\"Percentage \" + percentage\n          + \" must be greater than or equal to 0 \"\n          + \" and less than or equal to 100\");\n    }\n    if (maxMemory < 0) {\n      throw new HadoopIllegalArgumentException(\"Memory \" + maxMemory\n          + \" must be greater than or equal to 0\");\n    }\n    if (percentage == 0.0 || maxMemory == 0) {\n      return 0;\n    }\n    //VM detection\n    //See http://java.sun.com/docs/hotspot/HotSpotFAQ.html#64bit_detection\n    final String vmBit = System.getProperty(\"sun.arch.data.model\");\n\n    //Percentage of max memory\n    final double percentDivisor = 100.0/percentage;\n    final double percentMemory = maxMemory/percentDivisor;\n    \n    //compute capacity\n    final int e1 = (int)(Math.log(percentMemory)/Math.log(2.0) + 0.5);\n    final int e2 = e1 - (\"32\".equals(vmBit)? 2: 3);\n    final int exponent = e2 < 0? 0: e2 > 30? 30: e2;\n    final int c = 1 << exponent;","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L370-L406","documentation":"LightWeightGSet is the hash structure Hadoop uses for large NameNode maps (BlocksMap, INodeMap, CacheManager, RetryCache). computeCapacity() turns a percentage of a memory budget into an entry-table size. Before computing, it validates both inputs: percentage must be in [0.0, 100.0] and maxMemory must be >= 0; a negative maxMemory throws HadoopIllegalArgumentException. All production call sites use the public overload computeCapacity(percentage, mapName) which feeds Runtime.getRuntime().maxMemory() (always positive), so this throw comes from direct calls to the package-visible (long, double, String) overload or tests, as TestGSet does with computeCapacity(-1, 50.0, \"testMap\").","triggerScenarios":"Calling LightWeightGSet.computeCapacity(maxMemory, percentage, mapName) directly with maxMemory < 0; a custom GSet-sizing wrapper that parses a memory value from configuration (unset property defaulting to a -1 sentinel) and forwards it without a bounds check; unit tests enumerating invalid arguments.","commonSituations":"Writing unit tests for NameNode-side GSet sizing; porting the computeCapacity idiom into your own component with a config-sourced memory value; practically unreachable in stock Hadoop daemons because they pass Runtime.maxMemory().","solutions":["Pass a non-negative memory value, typically Runtime.getRuntime().maxMemory(), or use the public computeCapacity(percentage, mapName) overload","If the value comes from configuration, validate it at parse time and substitute a sane default instead of forwarding -1","Keep percentage within [0.0, 100.0]; note that 0.0 percentage or 0 maxMemory legally yields capacity 0"],"exampleFix":"// before\nlong mem = Long.parseLong(conf.get(\"mymap.maxmemory\", \"-1\"));\nint cap = LightWeightGSet.computeCapacity(mem, 25.0, \"mymap\");\n\n// after\nlong mem = conf.getLong(\"mymap.maxmemory\", Runtime.getRuntime().maxMemory());\nint cap = LightWeightGSet.computeCapacity(Math.max(0L, mem), 25.0, \"mymap\");","handlingStrategy":"validation","validationCode":"long maxMemory = Runtime.getRuntime().maxMemory(); // or config-sourced\ndouble pct = 25.0;\nif (maxMemory < 0) throw new IllegalArgumentException(\"maxMemory < 0: \" + maxMemory);\nif (pct < 0.0 || pct > 100.0) throw new IllegalArgumentException(\"pct out of range: \" + pct);\nint capacity = LightWeightGSet.computeCapacity(maxMemory, pct, \"myMap\");","typeGuard":null,"tryCatchPattern":"try { cap = LightWeightGSet.computeCapacity(mem, pct, \"myMap\"); } catch (HadoopIllegalArgumentException e) { throw new IllegalArgumentException(\"Bad capacity config for myMap: \" + e.getMessage(), e); }","preventionTips":["Prefer the public computeCapacity(percentage, mapName) overload which passes Runtime.maxMemory() for you","Validate config-derived memory values at load time; never forward -1 sentinels","Unit-test boundary values 0 and 100 for percentage and 0 for memory"],"tags":["hadoop","java","argument-validation","memory-config","gset"],"backgroundTag":"invalid-config-value","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}