Skip to content
Latchkey

openapi-generator "java.lang.OutOfMemoryError: Java heap space" in CI

OpenAPI Generator runs on the JVM. A very large or deeply nested spec can exhaust the default heap, so generation dies with "java.lang.OutOfMemoryError: Java heap space" partway through. Giving the JVM more heap usually resolves it.

What this error means

openapi-generator-cli aborts with "java.lang.OutOfMemoryError: Java heap space" mid-generation, often on a constrained runner with a big OpenAPI document.

openapi-generator
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at org.openapitools.codegen.DefaultGenerator.processPaths(...)

Common causes

Default JVM heap is too small for the spec

A large spec with many paths and schemas needs more heap than the JVM default, so generation runs out of memory.

A constrained runner with little memory

A small runner caps available RAM, leaving the JVM with too little to grow the heap.

How to fix it

Raise the JVM heap with JAVA_OPTS

  1. Set JAVA_OPTS (the CLI passes it to the JVM) to a larger maximum heap.
  2. Re-run generation with the increased limit.
  3. If it still fails, move to a larger runner.
Terminal
JAVA_OPTS="-Xmx4g" openapi-generator-cli generate \
  -i openapi.yaml -g typescript-axios -o ./client

Generate only what you need

Restrict generation to the relevant tags or models so the generator holds less in memory.

Terminal
openapi-generator-cli generate -i openapi.yaml -g go -o ./client \
  --global-property models,apis,supportingFiles

How to prevent it

  • Set an explicit -Xmx for large specs in CI.
  • Use a runner with enough memory for the JVM heap.
  • Split very large specs or limit generation scope.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →