# PackageCompiler.jl: display functions seem to use a lot of memory

**URL:** <https://discourse.julialang.org/t/packagecompiler-jl-display-functions-seem-to-use-a-lot-of-memory/95053>\
**Category:** General Usage\
**Created:** [February 23, 2023, 12:43am UTC](https://discourse.julialang.org/t/packagecompiler-jl-display-functions-seem-to-use-a-lot-of-memory/95053 "2023-02-23T00:43:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Post date:** [February 23, 2023, 12:43am UTC](https://discourse.julialang.org/t/packagecompiler-jl-display-functions-seem-to-use-a-lot-of-memory/95053/1 "2023-02-23T00:43:28Z")

</div>

MWE:

1. create a package with `PkgTemplates`
2. add following packages: `PackageCompiler` `Revise` `TimerOutputs`. Revise is not used, I just get used to add it to every package.
3. add the following code to `SelfTest.jl`:

```julia
using TimerOutputs

# Write your package code here.
function julia_main()::Cint
    to = TimerOutput()
    @timeit to "print" println("wow")
    show(to)
    return 0
end

```

1. add a `test/precompile.jl` file with the following content:

```julia
using SelfTest

SelfTest.julia_main()

```

1. create two apps with `PackageCompiler`, with the following command:

```julia
create_app(".", "bin/"; force=true, include_lazy_artifacts=true)
create_app(".", "bin_pre/"; precompile_execution_file=["test/precompile.jl"], force=true, include_lazy_artifacts=true)

```

Then begin test:

1. open a fresh Julia REPL, and call the function. First time will need precompilation, so I call it twice:

```julia
julia> using SelfTest
[Info: Precompiling SelfTest [5c836c24-3dcc-4429-8ed9-23232f434ad3]

julia> SelfTest.julia_main()
wow
 ────────────────────────────────────────────────────────────────────
                            Time Allocations      
                   ─────────────────────── ────────────────────────
 Tot / % measured: 602ms / 0.0% 46.9MiB / 0.0%    

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 print 1 52.0μs 100.0% 52.0μs 144B 100.0% 144B
 ────────────────────────────────────────────────────────────────────0

julia> SelfTest.julia_main()
wow
 ────────────────────────────────────────────────────────────────────
                            Time Allocations      
                   ─────────────────────── ────────────────────────
 Tot / % measured: 78.2μs / 81.4% 1.69KiB / 8.3%    

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 print 1 63.6μs 100.0% 63.6μs 144B 100.0% 144B
 ────────────────────────────────────────────────────────────────────0

```

1. use the compiled binaries:

```julia
(base) [user@machine selftest]$ ./bin/bin/SelfTest 
wow
 ────────────────────────────────────────────────────────────────────
                            Time Allocations      
                   ─────────────────────── ────────────────────────
 Tot / % measured: 602ms / 22.3% 81.0MiB / 17.9%    

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 print 1 134ms 100.0% 134ms 14.5MiB 100.0% 14.5MiB
 ────────────────────────────────────────────────────────────────────
(base) [user@machine selftest]$ ./bin_pre/bin/SelfTest 
wow
 ────────────────────────────────────────────────────────────────────
                            Time Allocations      
                   ─────────────────────── ────────────────────────
 Tot / % measured: 63.2ms / 100.0% 7.79MiB / 100.0%    

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 print 1 63.2ms 100.0% 63.2ms 7.79MiB 100.0% 7.79MiB

```

So, even the binary created with precompile\_execution\_file uses quite a lot memory. How can I improve it?

Something that may be of use:  
I run binary in this way to get compilation information:

```julia
./bin_pre/bin/SelfTest --julia-args --trace-compile=stderr

```

I got this:

```julia
precompile(Tuple{typeof(Base.Sys.which), String})
precompile(Tuple{typeof(Base.something), Nothing, String})
precompile(Tuple{typeof(Base.setindex!), Base.Dict{Base.PkgId, Revise.PkgData}, Revise.PkgData, Base.PkgId})
precompile(Tuple{typeof(Base.uv_timercb), Ptr{Nothing}})
precompile(Tuple{Revise.var"#107#108"})
precompile(Tuple{typeof(Printf.format), Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Base.Val{Char(0x66000000)}}}}, Float64})
precompile(Tuple{typeof(Base.notify), Base.GenericCondition{Base.Threads.SpinLock}, Any, Bool, Bool})
precompile(Tuple{typeof(Base._uv_hook_close), Base.Timer})
precompile(Tuple{typeof(Base.uvfinalize), Base.Timer})

```

---

<div class="post-metadata">

**Author:** ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Post date:** [February 23, 2023, 1:06am UTC](https://discourse.julialang.org/t/packagecompiler-jl-display-functions-seem-to-use-a-lot-of-memory/95053/2 "2023-02-23T01:06:52Z")

</div>

It seems to be related to `Revise`. Remove it and it is just fine:

```julia
[user@machine selftest]$ ./bin_pre_new/bin/SelfTest --julia-args --trace-compile=compile.log
wow
 ────────────────────────────────────────────────────────────────────
                            Time Allocations      
                   ─────────────────────── ────────────────────────
 Tot / % measured: 147μs / 89.6% 1.64KiB / 5.7%    

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 print 1 132μs 100.0% 132μs 96.0B 100.0% 96.0B

```

and `compile.log` contains this:

```julia
precompile(Tuple{typeof(Base.Sys.which), String})
precompile(Tuple{typeof(Base.something), Nothing, String})
precompile(Tuple{typeof(Printf.format), Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Base.Val{Char(0x66000000)}}}}, Float64})
precompile(Tuple{typeof(Printf.format), Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Base.Val{Char(0x66000000)}}}}, Int64})

```

Lessons learned: remember to exclude dev/debug utilities when compiling release binaries.
