{"record":{"id":"f5ef57d170ec2b1c","repo":"MyCATApache/Mycat-Server","slug":"could-not-determine-the-amount-of-free-memory-ple","errorCode":null,"errorMessage":"Could not determine the amount of free memory.\nPlease set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.","messagePattern":"Could not determine the amount of free memory\\.\nPlease set the maximum memory for the JVM, e\\.g\\. -Xmx512M for 512 megabytes\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/environment/EnvironmentInformation.java","lineNumber":116,"sourceCode":"\t/**\n\t * The maximum JVM heap size, in bytes.\n\t * \n\t * @return The maximum JVM heap size, in bytes.\n\t */\n\tpublic static long getMaxJvmHeapMemory() {\n\t\tlong maxMemory = Runtime.getRuntime().maxMemory();\n\n\t\tif (maxMemory == Long.MAX_VALUE) {\n\t\t\t// amount of free memory unknown\n\t\t\ttry {\n\t\t\t\t// workaround for Oracle JDK\n\t\t\t\tOperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();\n\t\t\t\tClass<?> clazz = Class.forName(\"com.sun.management.OperatingSystemMXBean\");\n\t\t\t\tMethod method = clazz.getMethod(\"getTotalPhysicalMemorySize\");\n\t\t\t\tmaxMemory = (Long) method.invoke(operatingSystemMXBean) / 4;\n\t\t\t}\n\t\t\tcatch (Throwable e) {\n\t\t\t\tthrow new RuntimeException(\"Could not determine the amount of free memory.\\n\" +\n\t\t\t\t\t\t\"Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.\");\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn maxMemory;\n\t}\n\n\t/**\n\t * Gets an estimate of the size of the free heap memory.\n\t * \n\t * NOTE: This method is heavy-weight. It triggers a garbage collection to reduce fragmentation and get\n\t * a better estimate at the size of free memory. It is typically more accurate than the plain version\n\t * {@link #getSizeOfFreeHeapMemory()}.\n\t * \n\t * @return An estimate of the size of the free heap memory, in bytes.\n\t */\n\tpublic static long getSizeOfFreeHeapMemoryWithDefrag() {\n\t\t// trigger a garbage collection, to reduce fragmentation","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/environment/EnvironmentInformation.java#L98-L134","documentation":"getMaxJvmHeapMemory estimates the maximum usable memory. On some JVMs Runtime.maxMemory() was not used, so it reflects com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize(); when that internal API cannot be loaded or invoked (any Throwable), it throws a RuntimeException telling the operator to set the JVM maximum memory explicitly (e.g. -Xmx512M).","triggerScenarios":"Class.forName(\"com.sun.management.OperatingSystemMXBean\") or its getMethod/invoke throws — running on a non-Oracle/OpenJDK JVM (J9, JRockit), a security-restricted environment, or a JVM lacking the com.sun.management API, while the code took the fallback path.","commonSituations":"Running mycat on IBM J9 or another JVM without com.sun.management.OperatingSystemMXBean; hardened JVM with reflection restricted; headless container images with minimal JDK; missing -Xmx in launch configuration.","solutions":["Set the JVM max heap explicitly at launch, e.g. java -Xmx512M ..., so the primary (Runtime-based) path is used","Switch to an Oracle/OpenJDK JVM that provides com.sun.management.OperatingSystemMXBean","Upgrade the JDK so the sun.management API is available","Wrap the call in try-catch and provide a configured default max-memory value as fallback"],"exampleFix":"// before\njava -jar mycat.jar\n// after\njava -Xmx512M -jar mycat.jar","handlingStrategy":"fallback","validationCode":"// pre-check the internal MXBean availability\ntry {\n    Class<?> c = Class.forName(\"com.sun.management.OperatingSystemMXBean\");\n    c.getMethod(\"getTotalPhysicalMemorySize\");\n} catch (Throwable t) {\n    // non-HotSpot JVM: ensure -Xmx is set so this path isn't needed\n}\nlong mx = Runtime.getRuntime().maxMemory();\nif (mx == Long.MAX_VALUE) log.warn(\"-Xmx not set\");","typeGuard":null,"tryCatchPattern":"long max;\ntry {\n    max = EnvironmentInformation.getMaxJvmHeapMemory();\n} catch (RuntimeException e) {\n    max = Runtime.getRuntime().maxMemory(); // fallback\n}","preventionTips":["Always launch with explicit -Xmx","Prefer HotSpot/OpenJDK runtimes","Check com.sun.management availability in deployment smoke tests","Document JVM requirements for operators"],"tags":["jvm","memory","environment"],"backgroundTag":"unsupported-platform","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}