Skip to content

Memory leak when instantiating arktype validators #1584

Closed

Description

@VanTanev

Report a bug

Memory leak when an arktype validator is instantiated, even with something as simple as type("string"), and anything else that instantiates a type, like type.pick() or type.and(other).

Memory does not leak on type.to(other), or type.keyof()

🔎 Search Terms

OOM, memory leak

🧩 Context

  • ArkType version: 2.1.29
  • TypeScript version (5.1+): 5.9.3
  • Other context you think may be relevant (JS flavor, OS, etc.):
    Node 24.13.0

🧑‍💻 Repro

// test-arktype-memory.ts
import { type } from "arktype";
// import { z } from "zod";

const BATCH = 1_000;
let i = 0;

while (true) {
  { 
    testArkType();
  }
  if (++i % BATCH === 0) {
    i = 0
    global.gc?.();
    console.log(
      `${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`,
    );
  }
}

// function testZod() {
//   z.string().transform((x) => parseInt(x)).parse('1');
// }

function testArkType() {
  type("string")('1');
}

Run with: node --expose-gc --no-warnings test-arktype-memory.ts

Activity

  1. pullfrog commented on Feb 6, 2026

    @pullfrog
    Contributor
  2. chris-kreidl commented on Feb 7, 2026

    @chris-kreidl
    Contributor

    I'm not a maintainer here, just a fan of the project, but I think this is expected behavior. Arktype registers every instantiated schema in a global registry, so if you create unbounded schemas that registry will grow unbounded as well.

    I don't think this is how the library was meant to be used. If you were to cache that schema outside of the loop you'd observe that no additional memory is consumed.

    For example, building on your demo:

    // test-arktype-memory.ts
    import { nodesByRegisteredId } from "@ark/schema";
    import { type } from "arktype";
    // import { z } from "zod";
    
    const BATCH = 1_000;
    const REUSE_SCHEMA = false;
    let i = 0;
    
    const cached = REUSE_SCHEMA ? type("string") : null;
    
    while (true) {
      { 
        testArkType();
        // testZod();
      }
      if (++i % BATCH === 0) {
        i = 0
        global.gc?.();
        console.log(
          `${Object.keys(nodesByRegisteredId).length} nodes`,
          `${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`,
        );
      }
    }
    
    // function testZod() {
    //   z.string().transform((x) => parseInt(x)).parse('1');
    // }
    
    function testArkType() {
      if (cached) return cached("1");
      type("string")("1");
    }
    

    Run once with REUSE_SCHEMA = false and again with REUSE_SCHEMA = true and observe the difference.

  3. eralmansouri commented on Feb 7, 2026

    @eralmansouri
    Contributor

    How would it be expected behavior to run out of memory?

  4. chris-kreidl commented on Feb 7, 2026

    @chris-kreidl
    Contributor

    How would it be expected behavior to run out of memory?

    I think it's unexpected behavior to instantiate millions of schemas.

    This is akin to putting a brick on your car's gas pedal, walking away, and asking why your engine blew up.

    If you use the library as intended then this is a non-issue.

  5. VanTanev commented on Feb 7, 2026

    @VanTanev
    Author

    I have confirmed with @ssalbdivad that this is indeed a bug and not intended behavior. As far as I'm aware, none of the other validation libraries suffer from a similar memory leak on schema instantiation.

  6. eralmansouri commented on Feb 7, 2026

    @eralmansouri
    Contributor

    How would it be expected behavior to run out of memory?

    I think it's unexpected behavior to instantiate millions of schemas.

    This is akin to putting a brick on your car's gas pedal, walking away, and asking why your engine blew up.

    If you use the library as intended then this is a non-issue.

    "You're holding it wrong."

    Does it really make sense to you that had to implement "caching" to avoid the library caching? What did the library cache, then, if each instantiation consumes more memory?

    I have confirmed with @ssalbdivad that this is indeed a bug and not intended behavior. As far as I'm aware, none of the other validation libraries suffer from a similar memory leak on schema instantiation.

    Thank you.

  7. JeremyMoeglich commented on Mar 30, 2026

    @JeremyMoeglich
    Contributor

    I had an incident where createParseContext slowed down to take several seconds (presumably because nodesByRegisteredId grew to a size where v8 has issues adding more keys) which slowed things down quite a bit.
    I was aware inline schema construction isn't optimal for performance but I assumed it's fine for routes that are rarely invoked wasn't aware that any schema even if dropped still leaks caches.

    It certainly is unintuitive I assumed schema construction to be mostly pure.

  8. added a commit that references this issue on Oct 1, 2026
    1ee4923
  9. added a commit that references this issue on Oct 2, 2026
    254328c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions