Skip to content

Extensive memory consumption when using OpenAPI 3.1 spec #3805

Description

@tarnowskijan

Q&A (please complete the following information)

  • OS: macOS
  • Environment: Node.js v10.9.2
  • Method of installation: npm
  • Swagger-Client version: 3.34.0
  • Swagger/OpenAPI version: OpenAPI 3.1

Content & configuration

Swagger/OpenAPI definition:
https://petstore31.swagger.io/api/v31/openapi.json

Swagger-Client usage:

const swaggerClient = require('swagger-client')

const specFile = require('./petstore-spec')

async function main() {
    const specVersion = process.argv[2]
    const executions = Number(process.argv[3] || '10')
    console.log(`Using spec version: ${specVersion}, executions: ${executions}`)

    const iterator = Array.from(Array(executions).keys());
    const spec = Object.assign(specFile, {openapi: specVersion})
    const specClients = await Promise.all(
        iterator.map(idx => swaggerClient({
            spec: Object.assign(
                {...spec},
                {servers: [{url: 'http://localhost:8989/example-api-' + idx}]}
            )
        }))
    )
    return specClients.length
}

main()
    .then(res => {
        console.log(res);
    })
    .catch(err => {
        console.error(err);
    })

Describe the bug you're encountering

After migration to OpenAPI 3.1 specs we are experiencing memory consumption spikes leading to OOMs in our Node.js applications. Our investigation revealed that creating Swagger clients from OpenAPI 3.1 spec requires about 20% more memory than it was for 3.0.1 spec. For large specs (186 kB) it's even six times more and we have plenty of such specs. Unfortunately, we cannot execute promises one-by-one to limit the consumption because it would take ages to build all clients (sometimes even 50).

Petstore spec:

% /usr/bin/time -l node app.js 3.0.1 40 
Using spec version: 3.0.1, executions: 40
40
        0.70 real         0.35 user         0.09 sys
           138412032  maximum resident set size

% /usr/bin/time -l node app.js 3.1.0 40
Using spec version: 3.1.0, executions: 40
40
        1.39 real         1.59 user         0.07 sys
           160399360  maximum resident set size

Large spec:

% /usr/bin/time -l node app.js 3.0.1 40
Using spec version: 3.0.1, executions: 40
40
        0.85 real         0.60 user         0.09 sys
           144490496  maximum resident set size

% /usr/bin/time -l node app.js 3.1.0 40
Using spec version: 3.1.0, executions: 40
40
       30.93 real        32.17 user         0.48 sys
           903905280  maximum resident set size

To reproduce...

Steps to reproduce the behavior:

  1. (Optional) Replace petstore-spec file with some large spec file
  2. Execute /usr/bin/time -l node app.js 3.0.1 40 to get results for 3.0.1 spec
  3. Execute /usr/bin/time -l node app.js 3.1.0 40 to get results for 3.1.0 spec

Expected behavior

Memory consumption for large 3.1.0 specs is as close as possible to 3.0.1 specs

Additional context or thoughts

Is there a way to configure swagger-client so it consumes less memory for 3.1.0 specs?

Activity

  1. ponelat commented on Feb 14, 2025

    @ponelat
    Contributor
  2. glowcloud commented on Feb 21, 2025

    @glowcloud
    Contributor

    After investigating, we have determined that the cause lies within the ApiDOM structure.

    The result of refracting a definition of 10 MB size will leave 100 MB of memory footprint for the JSON structure. For the ApiDOM AST interpretation, the footprint will be 200 MB, and the dehydrated result will take 400 MB.

    We need to further investigate the possibilities of amending the ApiDOM and its elements to address this issue.

  3. char0n commented on Mar 13, 2025

    @char0n
    Contributor

    ApiDOM is unfortunately memory inefficient. We're already trying to figure out how to address this.

  4. thomastrapanese commented on Aug 24, 2026

    @thomastrapanese

    Any updates? I'm trying to generate OpenAPI specs for an Angular project. The OpenAPI file is 130k lines long, and the generation requires a minimum of 22 GB of RAM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions