You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak when instantiating arktype validators #1584
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.tsimport{type}from"arktype";// import { z } from "zod";constBATCH=1_000;leti=0;while(true){{testArkType();}if(++i%BATCH===0){i=0global.gc?.();console.log(`${(process.memoryUsage().heapUsed/1024/1024).toFixed(2)} MB`,);}}// function testZod() {// z.string().transform((x) => parseInt(x)).parse('1');// }functiontestArkType(){type("string")('1');}
Run with: node --expose-gc --no-warnings test-arktype-memory.ts
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.
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.
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.
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.
Activity
pullfrog commentedon Feb 6, 2026
chris-kreidl commentedon Feb 7, 2026
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:
Run once with
REUSE_SCHEMA = falseand again withREUSE_SCHEMA = trueand observe the difference.eralmansouri commentedon Feb 7, 2026
How would it be expected behavior to run out of memory?
chris-kreidl commentedon Feb 7, 2026
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.
VanTanev commentedon Feb 7, 2026
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.
eralmansouri commentedon Feb 7, 2026
"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?
Thank you.
JeremyMoeglich commentedon Mar 30, 2026
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.
release: arktype 2.2.7 + dependents (#1675)
fix(schema): release unused parse context ids from the global registry (