MyCATApache/Mycat-Server · error · RuntimeException

Could not determine the amount of free memory. Please set…

Error message

Could not determine the amount of free memory.
Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.

What it means

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).

Solutions

  1. Set the JVM max heap explicitly at launch, e.g. java -Xmx512M ..., so the primary (Runtime-based) path is used
  2. Switch to an Oracle/OpenJDK JVM that provides com.sun.management.OperatingSystemMXBean
  3. Upgrade the JDK so the sun.management API is available
  4. Wrap the call in try-catch and provide a configured default max-memory value as fallback

Example fix

// before
java -jar mycat.jar
// after
java -Xmx512M -jar mycat.jar
Defensive patterns

Strategy: fallback

Validate before calling

// pre-check the internal MXBean availability
try {
    Class<?> c = Class.forName("com.sun.management.OperatingSystemMXBean");
    c.getMethod("getTotalPhysicalMemorySize");
} catch (Throwable t) {
    // non-HotSpot JVM: ensure -Xmx is set so this path isn't needed
}
long mx = Runtime.getRuntime().maxMemory();
if (mx == Long.MAX_VALUE) log.warn("-Xmx not set");

Try / catch

long max;
try {
    max = EnvironmentInformation.getMaxJvmHeapMemory();
} catch (RuntimeException e) {
    max = Runtime.getRuntime().maxMemory(); // fallback
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/f5ef57d170ec2b1c. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/memory/environment/EnvironmentInformation.java:116

	/**
	 * The maximum JVM heap size, in bytes.
	 * 
	 * @return The maximum JVM heap size, in bytes.
	 */
	public static long getMaxJvmHeapMemory() {
		long maxMemory = Runtime.getRuntime().maxMemory();

		if (maxMemory == Long.MAX_VALUE) {
			// amount of free memory unknown
			try {
				// workaround for Oracle JDK
				OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
				Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
				Method method = clazz.getMethod("getTotalPhysicalMemorySize");
				maxMemory = (Long) method.invoke(operatingSystemMXBean) / 4;
			}
			catch (Throwable e) {
				throw new RuntimeException("Could not determine the amount of free memory.\n" +
						"Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.");
			}
		}
		
		return maxMemory;
	}

	/**
	 * Gets an estimate of the size of the free heap memory.
	 * 
	 * NOTE: This method is heavy-weight. It triggers a garbage collection to reduce fragmentation and get
	 * a better estimate at the size of free memory. It is typically more accurate than the plain version
	 * {@link #getSizeOfFreeHeapMemory()}.
	 * 
	 * @return An estimate of the size of the free heap memory, in bytes.
	 */
	public static long getSizeOfFreeHeapMemoryWithDefrag() {
		// trigger a garbage collection, to reduce fragmentation

View on GitHub (pinned to 65f8d8beb7)