SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Java New Added 9 September 2026

Java: java.lang.OutOfMemoryError: Metaspace

Metaspace holds class metadata rather than objects, so raising the heap changes nothing. It fills when an application keeps loading classes: repeated hot redeploys inside one JVM, a proxy or scripting library generating a class per request, or a classloader kept alive by a single stray reference.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
# Watch the class count, not the heap
jcmd <pid> VM.native_memory summary | grep -A3 Class
jstat -gcmetacapacity <pid> 1000 5

# Which loaders are still alive, and what is holding them
jmap -clstats <pid>
jcmd <pid> GC.class_stats   # needs -XX:+UnlockDiagnosticVMOptions

# A ceiling turns a slow leak into a clear, early failure
java -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError \
     -XX:HeapDumpPath=/var/log/app -jar app.jar

# Hot redeploys leak loaders by design: restart the JVM per deploy
# and look for ThreadLocals and JDBC drivers pinning the old loader

How to diagnose Java errors

Java errors concentrate at the class loading boundary (ClassNotFoundException, NoClassDefFoundError, UnsupportedClassVersionError) and around resource pools under load. Class loading errors are almost always classpath or version problems rather than missing code. UnsupportedClassVersionError in particular is a pure bytecode-version mismatch and tells you exactly which JDK compiled the class.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Print the actual runtime version with java -version and compare it against your build target. Major bytecode version 65 is Java 21, 61 is Java 17, 52 is Java 8.
  2. Inspect the resolved dependency tree (mvn dependency:tree, gradle dependencies) to find duplicate or conflicting versions of the same library.
  3. For pool exhaustion, log pool metrics (HikariCP exposes active, idle and pending counts). Exhaustion means connections are not being returned, which is a try-with-resources problem.
  4. Enable -verbose:class temporarily to see which jar a class is loaded from when two versions are on the classpath.
  5. Capture a heap dump on OOM with -XX:+HeapDumpOnOutOfMemoryError and analyse it rather than raising -Xmx blindly.

Tools worth reaching for

  • mvn dependency:tree
  • jcmd / jstack / jmap
  • -verbose:class
  • Eclipse MAT
  • JFR (Java Flight Recorder)

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

docs.oracle.com

Related Java errors

See all 19 Java errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.