# Proposal for deterministic memory management

**URL:** <https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305>\
**Category:** Internals & Design\
**Created:** [May 10, 2020, 7:36am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305 "2020-05-10T07:36:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 11, 2020, 9:12pm UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/21 "2020-05-11T21:12:45Z")

</div>

I think that all of the removal and propagation can be preplanned and determined by the compiler. Why would this have less performance than a runtime GC?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 11, 2020, 9:18pm UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/22 "2020-05-11T21:18:30Z")

</div>

It cannot be preplanned because how things live and are referenced is dynamic. We don’t have any borrow checker like Rust and even then when you want to use certain dynamic things you opt into ref counting ([Rc in std::rc - Rust](https://doc.rust-lang.org/std/rc/struct.Rc.html)).

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 11, 2020, 9:40pm UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/23 "2020-05-11T21:40:15Z")

</div>

To add to this, maybe worth spelling out how you’ll handle an example like:

```julia
store, stored = let s = Any[]
    function store(x)
        push!(s, x)
        nothing
    end

    function stored()
        s
    end

    store, stored
end

function leak()
    x = [1, 2, 3]
    store(x)
    s = sum(x)
    s
end

leak()

stored()

leak()

stored()

```

It’s worth trying to implement that exact kind of code in Rust to get yourself thinking about the ownership issues explicitly.

---

<div class="post-metadata">

**Author:** ![Marc.Cox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc.cox/32/7514_2.png) [@Marc.Cox](https://discourse.julialang.org/u/Marc.Cox)\
**Post date:** [May 11, 2020, 10:47pm UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/24 "2020-05-11T22:47:27Z")

</div>

A brief History of Time 😉 I was there when Java was a little wannabe tyke called “Oak” lol ,and I saw and worked through its growing pains including the _ **interminable** _ GC pauses in Java v1. Honestly it was kinda an ughly baby duckling back then but BEA Weblogic Java **JRockit Real Time** GC is a beautiful swan now, so I hope Julia can get through that duckling to swan phase quickly lol.

So some food for thought about Industrial strength Java JVM Deterministic garbage collection strategies in production at large financial institutions today \>\>

Deterministic garbage collection is the ability to specify and maintain a maximum pause time for the memory system with a high level of confidence.

It is designed to deliver short, PREDICTABLE pause times – absolutely critical for trading systems.

.

There are no ideal garbage collectors,  
but the deterministic garbage collection functionality in **JRockit Real Time**  
represents a significant improvement over traditional methods.

> **[oracle-jrockit-real-time-1517310.pdf](https://www.oracle.com/a/ocom/docs/oracle-jrockit-real-time-1517310.pdf)**

More Alternatives - but NOTE Real Iffy about predictable pauses i.e. _ **NOT** _ determinstic \>\>

> **[Not all JVMS are created equal](https://www.dynatrace.com/resources/ebooks/javabook/the-three-jvms/)**
>
> In this eBook we'll examine some of the enterprise-level garbage-collection specifics of the three most prominent JVMs, like Oracle JRockit vs HotSpot JVM.

Related to ( I believe ) Stefan’s previous remark about LLVM mark-and-sweep algorithm ..

As of Java 5 (Note v5 is Very Old, latest is v13) , the IBM WebSphere JVM has added generational GC configuration option to its classic mark-and-sweep algorithm.

BTW @StefanKarpinski I don’t seek the anger of the ancient Norse gods, IOW I’m NOT griping 😄, I’m very glad Julia addressed GC early on in some fashion - and I assume this thread is about if there is any need for a new approach and forward looking so just wanted to add my two cents about what I know **actually works** in production at scale for large high transaction volume financial institutions today so Julia can successfully grow .

HTH,

Marc

Ps\> If not already, I also wonder about applying C Lint or its more advanced descendants to this issue \>\>

### Static automatic syntax analysis

Humans aren’t the only ones who can read source code, of course. You should also make _static syntax analysis_ part of your development process. Static syntax analysis is what `lint` , _strict compilation_ , and several commercial products do: Scan a source text and spot items that **a compiler accepts** , _but_ that are **likely to be symptoms of mistakes.**

Expect to make your code _lint-free_ . While `lint` is old and limited, the many programmers who don’t bother with it (or its more advanced descendants) **make a big mistake.**

> **[IBM Developer](https://developer.ibm.com/components/aix/)**

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 12, 2020, 2:16am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/25 "2020-05-12T02:16:08Z")

</div>

Besides all the technical issues (that I do not understand), is there any hope (or expectation) that Julia can be tweaked to be deterministic in terms of allocation? I do have amazing plans to embed Julia into Cubesats for attitude control purposes. However, those problems with allocation can be a no-go.

At least we now have good ways to avoid the compilation by calling all the methods at the startup. This memory issue might be the last barrier!

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 12, 2020, 5:25am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/26 "2020-05-12T05:25:59Z")

</div>

> [@Ronis\_BR](#):
>
> is there any hope (or expectation) that Julia can be tweaked to be deterministic in terms of allocation?

Good question. Somewhat more focused toward your actual application, I’d slightly reword to “What can be done so Julia can reliably be used in real time control?”

I think the answer is “Lots; Julia can be a really great platform for this in the future”. To expand, I think there’s two very different ways to attack this:

### Static compilation of a restricted Julia subset to native code for hard realtime

Allow a restricted subset of Julia (simple functions with concrete type signatures) to be compiled down to native code, with compile time checking of the ways in which the Julia runtime is used. For instance, you might instruct the compiler to verify that no dynamic allocation is used, or that now exceptions are thrown, or various other things.

In some sense this kind of works already (you can see with `@code_native` that many Julia functions are as tightly efficient as the corresponding function written in C). However there’s no convenient way to emit the native code into an external shared library. And no system for using the compiler to statically prove invariants about your code. Also I believe that Julia `Task`s running hard realtime control loops still need to pause for GC on other `Task`s, but this problem can be fixed in the future if the compiler can prove that the hard realtime loop allocates no memory.

I think proving invariants and having a user interface to require these invariants may be needed for the most demanding hard realtime control loops where there are time deadlines which absolutely must be met for safety and reliability. However it’s got the rather large downside that some core Julia constructs will not be available. Therefore Julia code written with this in mind will form its own subset of the wider Julia ecosystem.

Note that none of this requires fiddling with the language semantics or syntax. No need to add scoped lifetimes or any kind of RAII to the language — as others have pointed out, these features are difficult to reconcile with Julia’s semantics. Rather, you write your Julia code in a particular style, and ask the compiler to statically prove invariants about the native code which are required for your application.

### Low latency GC for soft realtime

Soft realtime — where GC and other latency is nearly always below some threshold — seems doable in the future without changing any language semantics. It’s significant engineering effort though, so I guess it’s a matter of time and resources. This is the approach that @Marc.Cox is referring to above: tweaks and optimizations to the Julia GC to have very low pause times on average.

## The larger picture

As I understand it, control systems often have a hard realtime loop embedded within a larger soft-realtime planning layer or layers. I think Julia can and will be a _really excellent_ platform for having all of this within one application and in one language. People like @rdeits already use it this way for robotic research applications and have for some time, but I suspect the tooling has a fair way to go before it will be widely acceptable in industry (perhaps Robin or someone else can correct me on that, though 🙂 )

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [May 12, 2020, 6:19am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/27 "2020-05-12T06:19:34Z")

</div>

I’d love to see Julia’s GC move in that direction where soft-realtime applications are possible.

I work in quantitative finance and already do most of my trading strategy research in Julia, but am hesitant to move my production execution systems to Julia until GC pauses can reliably be limited to no more than 1-2 milliseconds.

If Julia can do that, then it would truely be a single language solution for me.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 12, 2020, 7:27am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/28 "2020-05-12T07:27:02Z")

</div>

> [@c42f](#):
>
> However there’s no convenient way to emit the native code into an external shared library. And no system for using the compiler to statically prove invariants about your code.

The GPU people have done a lot of the work for this. In some sense, you would “just” need to change the target from e.g. PTX to x86-64

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 12, 2020, 7:36am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/29 "2020-05-12T07:36:26Z")

</div>

> [@kristoffer.carlsson](#):
>
> The GPU people have done a lot of the work for this. In some sense, you would “just” need to change the target from e.g. PTX to x86-64

Nice! Heh, that’s why I added that weasel word “convenient” 😆 I had a feeling that in some sense this was already possible… for certain people.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 12, 2020, 8:00am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/30 "2020-05-12T08:00:32Z")

</div>

> [@robsmith11](#):
>
> I’d love to see Julia’s GC move in that direction where soft-realtime applications are possible.

Here’s some really interesting reading for the journey to short pause times which the Go language runtime went through: [Getting to Go: The Journey of Go's Garbage Collector - The Go Programming Language](https://blog.golang.org/ismmkeynote). They were last targeting 0.5 ms pause times which seems pretty remarkable. Do you have a quantitative feeling for how large Julia’s GC latency would be in your use case?

The Go case is interesting because I think the Go runtime is very similar to ours in the macro aspects that are relevant. (Their concurrency model is similar and they also have a non-moving GC.) It would be super interesting to follow a similar journey for Julia, I suspect more a matter of having the time/money to resource the project than any technical problem.

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [May 12, 2020, 8:19am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/31 "2020-05-12T08:19:02Z")

</div>

> [@c42f](#):
>
> Here’s some really interesting reading for the journey to short pause times which the Go language runtime went through: [Getting to Go: The Journey of Go's Garbage Collector - The Go Programming Language](https://blog.golang.org/ismmkeynote). They were last targeting 0.5 ms pause times which seems pretty remarkable. Do you have a quantitative feeling for how large Julia’s GC latency would be in your use case?

I have seen that Go presentation. Very impressive results.

I wrote a simple market data handler in Julia a while ago and did some tests that resulted in pauses of up to 20 milliseconds. My existing solution could keep the pauses under 0.8 milliseconds. I’m not doing anything ultra high-frequency where I’d be counting single microseconds, but 20 milliseconds is a bit too much.

I tried to be careful with allocations in my test, but didn’t make much of an effort at a deeper dive. There’s probably some low hanging fruit with the TCP messaging.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 8:21am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/32 "2020-05-12T08:21:20Z")

</div>

> [@kristoffer.carlsson](#):
>
> We don’t have any borrow checker like Rust

I didn’t want to use complex lingo to make this part of Julia another [frustrating Rust](https://www.infoworld.com/article/3324488/rust-language-is-too-hard-to-learn-and-use-says-user-survey.html). The rules I set covers the ownership:

> Things coming from the outside of the scope (entry points), should not be removed inside the scope.  
> The variable returned from a function applied to the arguments with different scopes will have the outer scope.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 8:53am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/33 "2020-05-12T08:53:07Z")

</div>

Here this scope exports a function (like a small module) and is executed outside of that module, which I am not sure if we can handle this. See my global variable and generic programming section.

We may be able to handle this only if this is written in one module. When you return a function, and you expect the scoping information to be available outside, you should write everything in one module. I should note that “module wide memory management when the code includes global variables” is not the first priority of this proposal.

I think of this `let` that export functions as a limited global scope. It is like a small module. Since it has global variables and you are exporting functions. It is not like a function, that is callable. In my proposal, I explicitly stated that this kind of global variable should be deleted manually. Two functions with let scope are defined and returned, but called from outside of that scope. This does not change anything regarding its let\_global `s`.

```julia
store, stored = let s = Any[] # `s` is `Var{:let_global}`
    function store(x) # `x` coming from outside so should not be removed here
        push!(s, x) # `s` is still `Var{:let_global}`
        nothing
    end

    function stored()
        s # `s` comes from outside so it is `Var{:let_global}`
    end
   
    # let's s is an outsider to this scope. Unless removed from outside, it will be kept in the memory.
    store, stored # these functions are executed in `{:let_global}`
end

function leak()
    x = [1, 2, 3] # `x` is `Var{:leak_local}`
    store(x) # executed in `let_local`. `s` gets `[1,2,3]`
    s = sum(x) # `s` becomes `Var{:leak_local}` with propagation. now `s` and `x` should be removed together
    s # `s` is returned so it is not removed (same for its associated `x`).
end 

leak() # return is not stored so leak's `s` and `x` will be removed here

stored() # executed in `let_global`. In that scope `s` was let_global so it is not removed.

leak() # return is not stored so leak's `s` and `x` will be removed here

stored() # executed in `let_global`. In that scope `s` was let_global so it is not removed.

# let's `s` was global so it is not removed here. 

```

You are writing a kind of code I have never written in my lifetime. If it was me, instead of thinking about ownership in Rust, I would change my coding style. I am not afraid to say this system cannot handle all the valid Julia codes. Also, see the next post.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 9:04am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/34 "2020-05-12T09:04:53Z")

</div>

We can do this in a backward-compatible manner. By **only managing the memory for the variables** that are defined using `Var{:scope}()` for named scopes and `Var()` for local scopes.

We can easily set limitations for the developers then. They should not call `@eval` in a scope that uses `Var` (we can return an error), they should manage global variables manually, and **any other situation** that we think we cannot handle automatically.

By setting these limitations I cannot think of any code that breaks. Either someone wants automatic memory management and follows the limitations or they should use GC.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 9:21am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/35 "2020-05-12T09:21:57Z")

</div>

> [@c42f](#):
>
> Good question. Somewhat more focused toward your actual application, I’d slightly reword to “What can be done so Julia can reliably be used in real time control?”
> 
> I think the answer is “Lots; Julia can be a really great platform for this in the future”. To expand, I think there’s two very different ways to attack this:
> 
> ### Static compilation of a restricted Julia subset to native code for hard realtime
> 
> Allow a restricted subset of Julia (simple functions with concrete type signatures) to be compiled down to native code, with compile time checking of the ways in which the Julia runtime is used. For instance, you might instruct the compiler to verify that no dynamic allocation is used, or that now exceptions are thrown, or various other things.
> 
> In some sense this kind of works already (you can see with `@code_native` that many Julia functions are as tightly efficient as the corresponding function written in C). However there’s no convenient way to emit the native code into an external shared library. And no system for using the compiler to statically prove invariants about your code. Also I believe that Julia `Task` s running hard realtime control loops still need to pause for GC on other `Task` s, but this problem can be fixed in the future if the compiler can prove that the hard realtime loop allocates no memory.

That is my goal and I hope we can help to achieve that. Exporting the native code from Julia is being done in [StaticCompiler.jl](https://github.com/tshort/StaticCompiler.jl). [Its limitations](https://github.com/tshort/StaticCompiler.jl#known-limitations) are totally fine to me. Neither we can cover all the grounds, nor we should limit ourselves for doing that.  
Even in C++, there are specific guidelines and limitations that people should follow to write a very tight program. See this for example:  
[Embedded Template Library - A C++ template library for embedded applications](https://www.etlcpp.com/)

> [@c42f](#):
>
> ### Low latency GC for soft realtime
> 
> Soft realtime — where GC and other latency is nearly always below some threshold — seems doable in the future without changing any language semantics. It’s significant engineering effort though, so I guess it’s a matter of time and resources. This is the approach that @Marc.Cox is referring to above: tweaks and optimizations to the Julia GC to have very low pause times on average.

These two sound like two different paths to me. We should follow both. Improving GC helps the desktop users that don’t need realtime performance. Automatic management helps the other group that wants to use Julia in the realtime applications.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 12, 2020, 9:37am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/36 "2020-05-12T09:37:25Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> Things coming from the outside of the scope (entry points), should not be removed inside the scope.  
> The variable returned from a function applied to the arguments with different scopes will have the outer scope.

How about when you store objects created inside a scope into an object that outlives the scope (note this is a dynamic behavior and cannot be statically known)? The system needs some way to know if the memory for that object can be freed when the scope exit. This brings us back to reference counting again, see [std::shared\_ptr - cppreference.com](https://en.cppreference.com/w/cpp/memory/shared_ptr) for it’s use in yet another language.

> [@Amin\_Yahyaabadi](#):
>
> You are writing a kind of code I have never written in my lifetime. If it was me, instead of thinking about ownership in Rust, I would change my coding style.

The soundness of the system cannot depend on a certain coding style though, right?

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 9:43am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/37 "2020-05-12T09:43:04Z")

</div>

> [@kristoffer.carlsson](#):
>
> How about when you store objects created inside a scope into an object that outlives the scope (note this is a dynamic behavior and cannot be statically known)? The system needs some way to know if the memory for that object can be freed when the scope exit. This brings us back to reference counting again, see [std::shared\_ptr - cppreference.com](https://en.cppreference.com/w/cpp/memory/shared_ptr) for it’s use in yet another language.

When one object is stored in another object with different scope, the outer scope should be chosen for both. If one is from outside the other one should live until the outer one lives.

> [@kristoffer.carlsson](#):
>
> The soundness of the system cannot depend on a certain coding style though, right?

Yes, I am not afraid to say that this system cannot handle all the valid Julia codes. See my [comment here](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/34)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 12, 2020, 9:55am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/38 "2020-05-12T09:55:24Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> When one object is stored in another object with different scope, the outer scope should be chosen for both. If one is from outside the other one should live until the outer one lives.

Okay, so you need some kind of “manager” that keeps track of every objects actual “scope”? And this manager runs at every scope exit to clean up the objects that are no longer referenced? Sounds a bit like the current GC except it runs on every scope exit. Won’t that be slow?

Do you have any links or reference to any other programming language incorporating this strategy or is this a novel algorithm for automatic deterministic memory management?

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [May 12, 2020, 10:05am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/39 "2020-05-12T10:05:31Z")

</div>

> [@kristoffer.carlsson](#):
>
> Okay, so you need some kind of “manager” that keeps track of every objects actual “scope”? And this manager runs at every scope exit to clean up the objects that are no longer referenced? Sounds a bit like the current GC except it runs on every scope exit. Won’t that be slow?

Exactly. I think the compiler can plan this.

The runtime slowness depends on the user. If the user wants a variable to be removed when the scope exits, the compiler should obey. I can argue that overusing of scopes can be harmful. This has a similar overhead that calling a function has.

In some cases, the compiler may be able to optimize this by deferring the removal (if it sees that a lot of memory is available, and this is safe).

> [@kristoffer.carlsson](#):
>
> Do you have any links or reference to any other programming language incorporating this strategy or is this a novel algorithm for automatic deterministic memory management?

Not exactly. I think Beef, C++, and Rust are good places to start.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 12, 2020, 10:14am UTC](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305/40 "2020-05-12T10:14:00Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> Exactly. I think the compiler can plan this.

No, I mean… it is dynamic. Which is why it needs to be a “manager” that run alongside the code and keep track of stuff during runtime. Unless you have a “borrow checker” the lifetime of objects is not statically known.

> [@Amin\_Yahyaabadi](#):
>
> Not exactly. I think C++, Beef, and Rust are good places to start.

Anything particular in those languages? Rust has a borrow checker and drop ([Drop in std::ops - Rust](https://doc.rust-lang.org/std/ops/trait.Drop.html)) values when they go out of scope but there is also reference counting objects to into that when needed.  
C++ has stuff like RAII but for handling dynamic lifetimes you can use `unique_ptr` and `shared_ptr` which can kinda be mapped to a borrow checker (only one owner) and reference counting respectively.

None of these feel really applicable here so perhaps you could elaborate.

[Previous page](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305.md?page=1)

[Next page](https://discourse.julialang.org/t/proposal-for-deterministic-memory-management/39305.md?page=3)
