# How does Julia handle precompilation?

**URL:** <https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 10, 2025, 1:27pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251 "2025-09-10T13:27:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [September 10, 2025, 1:27pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/1 "2025-09-10T13:27:14Z")

</div>

I am trying to make an app using PackageCompiler.jl  
I would like to use a `precompile_statements_file`.

My understanding is that I basically call

```bash
julia --trace-compile=precompile_statements.jl --project=@. precompile.jl

```

and get a file `precompile_statements.jl` with lots of `precompile` statements.  
However, how can I make Julia compile all dependencies, the file seems too short?  
I tried `--compile=all` but that seems not to be it. And I worry that there are some missed and not part of the system image.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 10, 2025, 1:49pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/2 "2025-09-10T13:49:15Z")

</div>

A dumb way to do it is to clear the entire precompile cache, which is in `[julia depot]/compiled/v1.X`, where `[julia depot]` is the root of your depot (by default `~/.julia` unless you changed it), and `v1.X` is the minor version you’re using.

It’s safe to delete the precompile cache, the only obvious consequence is that you’ll have to precompile all packages whose cache you delete, but you do want to do that (or at least for a fraction of the packages already precompiled in your depot)

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [September 10, 2025, 4:27pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/3 "2025-09-10T16:27:07Z")

</div>

My motivation is more in the direction of how to cache the precompilation of the PackageCompiler.jl .

My thinking is that if I have the precompile statements, for some changes this is enough and I do not need to run `--trace-compile` again and I can skip the `precompile_execution_file` step. The sysimage part of the app building process is taking long enough.

What happens with stuff that is missed, i.e. has no preccompile statement at runtime? Can this be compiled later? How does this work, when there is no source?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 11, 2025, 10:41am UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/4 "2025-09-11T10:41:16Z")

</div>

> [@ohmsweetohm1](#):
>
> What happens with stuff that is missed, i.e. has no preccompile statement at runtime?

It’s not really clear what you’re describing here after talking about dependencies earlier. Do you mean call signatures that just happened to not execute in `precompile.jl`? For example, `rand((true, false)) ? foo(1) : foo(1.0) end` would randomly pick one call signature per run. In that case, the missed call signature can be JIT compiled later because `PackageCompiler` doesn’t trim anything from the bundled Julia runtime. The trim option of the experimental juliac does, so you’ll want to firmly specify everything you’ll call there like any other AOT-compiled language.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [September 11, 2025, 5:13pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/5 "2025-09-11T17:13:13Z")

</div>

When I run trace-compile on a script some things get recorded by my feeling is that this is not everything that is used, only the stuff that needed compilation. And my fear is that this is a problem.

> [@Benny](#):
>
> In that case, the missed call signature can be JIT compiled later because `PackageCompiler` doesn’t trim anything from the bundled Julia runtime.

But how does this work without the source?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 11, 2025, 8:05pm UTC](https://discourse.julialang.org/t/how-does-julia-handle-precompilation/132251/6 "2025-09-11T20:05:38Z")

</div>

JIT compilation doesn’t start from source code, it starts from instantiated methods and global variables. Those objects are serialized and stored in system images and package images. Prior to v1.9 that introduced native code caching to precompiled package images, they mostly stored this.

> [@ohmsweetohm1](#):
>
> not everything that is used, only the stuff that needed compilation

Not 100% certain, but I think you’re right that already-compiled calls executed during the script would not provide precompile statements. However, those _ought_ to be bundled by `PackageCompiler` as well. For example, any compiled native code from the default sysimage or precompiled packages are bundled; not sure if there’s another source of compiled code to worry about.
