# Large codebase and compile-time memory usage

**URL:** <https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327>\
**Category:** Help & Support\
**Created:** [November 10, 2026, 4:15am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327 "2026-11-10T04:15:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anykeyh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/anykeyh/32/726_2.png) [@anykeyh](https://forum.crystal-lang.org/u/anykeyh)\
**Post date:** [November 10, 2026, 4:15am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/1 "2026-11-10T04:15:16Z")

</div>

Hello,

I’m wondering how do you deal with large memory usage (multiple gigabytes) while compiling large codebase?

When running `crystal spec`, the app is compiled before run, and the memory used during compile time is kept when the test suite is run. It eventually get released while running, slowly.

Currently, my codebase (200k LOC) is reserving about 14 Gb while compiling (non-release). This cause a bit of headache with CI/CD target.  
I don’t use any macro, and I don’t think I use weird idiom in the code (I might).

---

<div class="post-metadata">

**Author:** ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)\
**Post date:** [November 10, 2026, 5:20am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/2 "2026-11-10T05:20:12Z")

</div>

Personally I don’t believe compile time mem usage is directly proportional to LOC. There might be something code related which is causing LLVM to take long time e.g large static arrays are notorious for same.

Try to add `--stats` flag to `crystal spec` or `build` to see the metrics and that might give you some further insights.

HIH

//Ali

---

<div class="post-metadata">

**Author:** ![anykeyh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/anykeyh/32/726_2.png) [@anykeyh](https://forum.crystal-lang.org/u/anykeyh)\
**Post date:** [November 10, 2026, 5:50am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/3 "2026-11-10T05:50:06Z")

</div>

I am realizing it’s because I’m using the `--debug` flag on specs. Without it, I top at 4Gb for the compilation process, and ~14Gb with it.  
Interestingly, the debug information is not displayed with `--stats`:

```auto
crystal spec --debug --stats
Parse: 00:00:00.000733101 ( 1.79MB)
Semantic (top level): 00:00:01.860505843 ( 416.05MB)
Semantic (new): 00:00:00.009226504 ( 416.05MB)
Semantic (type declarations): 00:00:00.249920548 ( 424.05MB)
Semantic (abstract def check): 00:00:00.066764536 ( 464.05MB)
Semantic (restrictions augmenter): 00:00:00.048750621 ( 464.05MB)
Semantic (ivars initializers): 00:00:00.738955685 ( 536.11MB)
Semantic (cvars initializers): 00:00:00.087541121 ( 536.11MB)
Semantic (main): 00:00:19.378652585 (3040.01MB)
Semantic (cleanup): 00:00:00.002303674 (3040.01MB)
Semantic (recursive struct check): 00:00:00.006614985 (3040.01MB)
Codegen (crystal): 00:00:33.193440948 (4080.03MB)
Codegen (bc+obj): 00:01:16.707523013 (4080.03MB)
Codegen (linking): 00:00:08.314692886 (4080.03MB)

Macro runs:
 - /usr/lib/crystal/ecr/process.cr: reused previous compilation (00:00:00.006013997)

Codegen (bc+obj):
 - no previous .o files were reused

```

---

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [November 10, 2026, 12:26pm UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/4 "2026-11-10T12:26:40Z")

</div>

As part of [https://sheety.ralsina.me](https://sheety.ralsina.me) I wrote a massive crystal generator. As in, it generates many thousand lines of code. If it’s all in one file it would crash any computer when building.

I ended making it split the source code in multiple files, and then the build is fast.

Of course if you use `--release` it will re-merge all the codebase (including ALL THE STANDARD LIBRARY YOU USE) into a single file for better optimization, which will, again, make it super slow.

Another thing that makes compilation very slow is [code with static arrays is very very slow to compile with the --release flag · Issue #2485 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/2485)

So, there are multiple pitfalls.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 10, 2026, 1:46pm UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/5 "2026-11-10T13:46:41Z")

</div>

FWIW macros get a bad wrap, generally it’s not as big of a perf impact as people think. It really just depends on _what_ you’re doing with them. Some things here and there? Prob fine. Generating 100s of new types? Going to have an impact, but also likely a similar impact as if you manually defined them yourself.

The more code you have the longer compile times will be. However this is generally linear and somewhat expected. However in my exp there are some things that can happen that result in non-linear compile time growth. Mainly as it relates to generics and/or large unions. Are you making use of generic types at all? Are they a part of large unions?

I have been making some improvements upstream to handle some of the common cases. Could you try the latest nightly version and see what, if any, difference that makes on your `crystal spec --stats` run?

EDIT: Also would it be possible for you to share the code privately with me? I can take a better look at things to see if there is any low hanging fruit if so.

---

<div class="post-metadata">

**Author:** ![anykeyh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/anykeyh/32/726_2.png) [@anykeyh](https://forum.crystal-lang.org/u/anykeyh)\
**Post date:** [November 11, 2026, 4:05am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/6 "2026-11-11T04:05:40Z")

</div>

The application will be released soon under source available license (basically Open source but with guardrails against parasitic behavior).  
I will try to find some time to experiment this week-end; and share with you once it’s published ;-).

---

<div class="post-metadata">

**Author:** ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)\
**Post date:** [November 13, 2026, 7:55am UTC](https://forum.crystal-lang.org/t/large-codebase-and-compile-time-memory-usage/9327/7 "2026-11-13T07:55:46Z")

</div>

We use make targets in the compiler to split building from running the specs, to avoid the memory issue. Roughly:

```makefile
std_spec: .build/std_spec
  .build/std_spec

.build/std_spec:
  mkdir -p .build
  $(CRYSTAL) build $(find spec/std -name "*_spec.cr") -o $@

```
