zod icon indicating copy to clipboard operation
zod copied to clipboard

zod v4.1.13 causes JavaScript heap out of memory

Open JakubCerovsky opened this issue 1 month ago • 2 comments

Since there was no action happening in the previously closed issue. I am opening a new one and referencing the last comment. I am experiencing the same issue as mentioned even with the latest zod version.

@colinhacks It seems like the issue persists—would it be possible to reopen this or should I open a brand new issue?

I am experiencing the same issue after the update to zod V4. The first screenshot is tests run with zod: 3.25.74 with maximum heap size reaching 855MB, while once I bumped to zod: 4.1.12 the heap size usage increases drastically resulting in failures in GitHub Actions since they allocate by default only ~2GB.

These screenshots shows the differences in heap usage sizes between mentioned versions.


Zod 3.25.74

Image

Zod 4.1.12

Image

Originally posted by @ghost in #5204

JakubCerovsky avatar Dec 01 '25 10:12 JakubCerovsky

Hi @JakubCerovsky! I'm Dosu and I’m helping the zod team.

I'm a bot trained on Zod's codebase and issues. This increased heap usage and out-of-memory problem in Zod v4 is a known and ongoing concern, with several users reporting 5–6x higher memory usage compared to v3, especially when loading large schemas. Some memory leaks were fixed in v4.1.6 (for legacy TypeScript configs) and v4.1.8 (for the global registry leak with .describe/.meta), but users—including those on v4.1.13—still report OOM errors in CI environments like GitHub Actions, and the root cause isn't fully resolved for all codebases [#5204].

Certain schema patterns, like using recursive schemas with .meta({id: ...}) inside getters, are known to cause excessive memory growth. If your code uses these patterns, try defining schemas outside recursive contexts and avoid .meta({id: ...}) in recursive getters [#5241].

If you haven't already, please share a minimal, self-contained reproduction—this is essential for maintainers to diagnose and address the issue, as many reports are highly project-specific and not widely reproducible. If your problem isn't related to the legacy TypeScript config or the global registry leak, opening a new issue with a reproduction is encouraged [#5204].

Workarounds discussed include switching to import { z } from 'zod/v4' (though this can break compatibility with some libraries), using package resolutions to force a single Zod version, and upgrading dependencies that may pull in Zod v3. Increasing Node.js memory limits is only a temporary fix.

Let me know if you can provide a reproduction or if you have more details about your schema patterns. If this answers your question, feel free to close the issue.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

dosubot[bot] avatar Dec 01 '25 10:12 dosubot[bot]

The repro from the linked issue was fixed, so this is a new issue that needs a new reproduction.

colinhacks avatar Dec 02 '25 00:12 colinhacks

In my case, my work project had two different versions of zod - one as a dependency, and the other as a dep of a dep. I suspect they were somehow conflicting, so it might not be an issue in zod in my case.

mikavilpas avatar Dec 15 '25 06:12 mikavilpas