# Large allocations with MTK event

**URL:** https://discourse.julialang.org/t/large-allocations-with-mtk-event/133680
**Category:** Performance
**Tags:** modelingtoolkit, events, differentialequation, diffeqcallbacks
**Created:** [November 5, 2025, 12:14pm UTC](https://discourse.julialang.org/t/large-allocations-with-mtk-event/133680 "2025-11-05T12:14:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![klinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klinders/32/219046_2.png) [@klinders](https://discourse.julialang.org/u/klinders)
#### Post date: [November 5, 2025, 12:14pm UTC](https://discourse.julialang.org/t/large-allocations-with-mtk-event/133680/1 "2025-11-05T12:14:04Z")

</div>

Dear all,

Recently, you helped me get events working in MTK. Thank you for all the help!

Continuing on the topic, I ran into a performance issue with events. I have the following code to measure the time and allocations for a system with and without events.

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

function affect!(mod,obs,ctx,int)
    ModelingToolkit.terminate!(int)

    return (;)
end

function FOL_event(;name)
    @parameters begin
        τ = 3.0 # parameters
    end
    @variables begin
        x(t) = 0.0 # dependent variables
    end
    eq=[
        D(x) ~ (1 - x) / τ
    ]

    events = [x ~ 0.6]=>(affect!,(;))

    return System(eq, t; name=name, continuous_events=events)
end

function FOL(;name)
    @parameters begin
        τ = 3.0 # parameters
    end
    @variables begin
        x(t) = 0.0 # dependent variables
    end
    eq=[
        D(x) ~ (1 - x) / τ
    ]

    return System(eq, t; name=name)
end

using OrdinaryDiffEq
@mtkbuild fol = FOL()
@mtkbuild fol_event = FOL_event()

print("Starting problems\n")
print("No event:")
@time prob = ODEProblem(fol, [], (0.0, 10.0), dense=true,save_everystep=false)
print("With event:")
@time prob_event = ODEProblem(fol_event, [], (0.0, 10.0), dense=true, save_everystep=false)

print("Solving problems\n")
print("No event:")
@time sol = solve(prob,Tsit5())
print("With event:")
@time sol_event = solve(prob_event, Tsit5())

```

If I run the code for the second time to skip compilation, I get the following output:

```julia-auto
Starting problems
No event: 0.009388 seconds (39.02 k allocations: 2.201 MiB)
With event: 0.007077 seconds (36.27 k allocations: 1.683 MiB)
Solving problems
No event: 0.000044 seconds (88 allocations: 6.578 KiB)
With event: 0.119570 seconds (144.21 k allocations: 8.894 MiB, 99.95% compilation time: 100% of which was recompilation)

```

With the events, the solving takes much longer and uses much more memory.

The topic seems similar to [Significant allocations with Callbacks (Tsit5)](https://discourse.julialang.org/t/significant-allocations-with-callbacks-tsit5/66467) , but since those fixes were merged a long time ago, I decided to open a new issue.

Im running: Julia 1.10.10, MTK 10.26.1, and OrdinaryDiffEq 6.103.0

I look forward to hearing your ideas.

Kind regards,

Koen Linders

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 5, 2025, 11:35pm UTC](https://discourse.julialang.org/t/large-allocations-with-mtk-event/133680/2 "2025-11-05T23:35:22Z")

</div>

This should get an issue.
