# Is it possible to manually manage the memory without having to use C Standard Library?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881>\
**Category:** General Usage\
**Created:** [November 7, 2022, 2:17pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881 "2022-11-07T14:17:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 7, 2022, 2:17pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/1 "2022-11-07T14:17:56Z")

</div>

If I want maximum performance for a program written in Julia, one of my options would be manually managing the memory with help of [Julia C Standard Library](https://docs.julialang.org/en/v1/base/libc/#C-Standard-Library).

But is it possible to do that in another way? Has Julia facilities for manually manage the memory?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [November 7, 2022, 2:21pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/2 "2022-11-07T14:21:31Z")

</div>

In summary no. Ideally if you want maximum performance you don’t want to allocate in your hot path at all.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 7, 2022, 8:55pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/3 "2022-11-07T20:55:55Z")

</div>

> [@gbaraldi](#):
>
> don’t want to allocate in your hot path at all.

And if you’re not allocating in the path then you can just use preallocated Julia memory, Array and such

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 7, 2022, 9:43pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/4 "2022-11-07T21:43:16Z")

</div>

> [@phantomcraft](#):
>
> Has Julia facilities for manually manage the memory?

Depends what you mean by “manage”.

Do you mean pre-allocate memory and re-use it? Sure, Julia provides lots of in-place operations that act on existing arrays and other data structures. You can even allocate storage for one type and re-use it for another type using `reinterpret`.

Do you mean working avoiding garbage collection entirely and manually free-ing memory as needed? Yes, you can allocate your own memory by `malloc` and use raw pointers (or `unsafe_wrap` to treat them as arrays/etcetera), but it obviously takes a lot more care. Usually this is not worth it except to interact with external non-Julia libraries.

Do you mean swapping in a custom garbage-collection algorithm for standard Julia objects? Not really, without recompiling Julia itself and doing a lot of low-level hacking. Of course, you can turn GC off completely with `GC.enable(false)`, and manually invoke it with `GC.gc()`.

---

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 8, 2022, 4:32pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/5 "2022-11-08T16:32:44Z")

</div>

I read on an article that D, Nim and Rust can be as fast as C if managing the memory manually, I don’t believe that managing the memory manually should be restricted to interacting with non-Julia libraries only.

I didn’t know that GC can be disabled, good to know.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 8, 2022, 4:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/6 "2022-11-08T16:37:57Z")

</div>

That article was certainly mistaken, as there’s no need for manually managing memory (i.e., calling `malloc` and `free` by hand) in Rust.

While disabling GC is an option, writing your code in a way that doesn’t require GC to run much (if at all) by reducing allocations in the first place, is as good as managing memory manually with `malloc` et al.

---

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 8, 2022, 5:13pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/7 "2022-11-08T17:13:41Z")

</div>

Here is the article: [https://github.com/frol/completely-unscientific-benchmarks/blob/master/README.md#observations](https://github.com/frol/completely-unscientific-benchmarks/blob/master/README.md#observations)

/\ “Nim, D, and Rust can go as fast as C/C++ can when you switch to “unsafe” manual memory management.”

> While disabling GC is an option, writing your code in a way that doesn’t require GC to run much (if at all) by reducing allocations in the first place, is as good as managing memory manually with `malloc` et al.

Is that I want to do in the future (I’m still studying programming).

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 8, 2022, 5:40pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/8 "2022-11-08T17:40:20Z")

</div>

I had never heard of that benchmark so I went and looked and it describes itself as being a benchmark primarily for memory management intensive operations

For the most part that’s just not true of most real world problems that you would choose to use Julia for so I don’t think it’s particularly relevant to decision making

It’s definitely the case that memory management can slow down your programs but there are many many ways to account for that within Julia without resorting to malloc

---

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 8, 2022, 6:02pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/9 "2022-11-08T18:02:49Z")

</div>

I am oldstyle, I prefer to manage the memory manually.

The thing that made me choose Julia is String macros: [Five (More) Reasons to Check Out Julia](https://www.manhattanmetric.com/blog/2018/01/five-more-reasons-to-check-out-julia.html)

Did you remember when you was a very young child and you played with play dough? I can do the same with Julia macros and its memory management facilities.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 8, 2022, 6:04pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/10 "2022-11-08T18:04:00Z")

</div>

> [@phantomcraft](#):
>
> oldstyle, I prefer to manage the memory manually.

There are many ways to do this in Julia without resorting to malloc

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [November 8, 2022, 6:09pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/11 "2022-11-08T18:09:53Z")

</div>

It’s usually a mistake to avoid allocations (they are not slow), or to use Libc.malloc directly.

When you allocate indirectly the same Libc.malloc is used (and same or better speed). For real-time, you want to avoid GC triggered, by avoiding allocations in parts of your program (a main loop). Even if you want to manage your own memory call Libc.malloc, there are packages you may want to use rethar than Julia’s standard library API directly. See e.g. StaticTools.jl.

Avoiding Libc.malloc entirely, directly or indirectly is problematic. Then you have no heap.

I theory you can get memory straight from the kernel (e.g. Linux’s or Windows’s etc), but your program wouldn’t be portable that way. You really want to use malloc in libc at least indirectly for portability.

There are alternative malloc than in the default libc, e.g. mimalloc and there’s a PR to make it work in Julia. It’s a good replacement. There are also alternative libc available, see musl Julia, and Cosmobilitan is interesting but not yet supported…

---

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 8, 2022, 6:30pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/12 "2022-11-08T18:30:11Z")

</div>

> There are many ways to do this in Julia without resorting to malloc

Yes, I’m going to study them all.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 8, 2022, 6:34pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/13 "2022-11-08T18:34:16Z")

</div>

In [GitHub - mkitti/ArrayAllocators.jl: Allocate arrays with malloc, calloc, or on NUMA nodes](https://github.com/mkitti/ArrayAllocators.jl) I put together some methods of allocating and wrapping them in arrays.

For example on Windows, I wrap `VirtualAllocEx`: [https://github.com/mkitti/ArrayAllocators.jl/blob/455341fcd3ac20865d454b9cbfdc0841bf8b333f/src/Windows.jl#L106-L109](https://github.com/mkitti/ArrayAllocators.jl/blob/455341fcd3ac20865d454b9cbfdc0841bf8b333f/src/Windows.jl#L106-L109)

On Linux, I wrap `posixmemalign`:

> <https://github.com/mkitti/ArrayAllocators.jl/blob/455341fcd3ac20865d454b9cbfdc0841bf8b333f/src/POSIX.jl#L37>

I usually add a `finalizer` to these so automatically free the memory, but you do need to do that if you wish to manually free it.

You mentioned strings. I’ve been working on static memory allocation in InlineStrings.jl and StaticStrings.jl including implement string macros.

> **[GitHub - JuliaStrings/InlineStrings.jl: Fixed-width string types for Julia](https://github.com/JuliaStrings/InlineStrings.jl)**
>
> Fixed-width string types for Julia. Contribute to JuliaStrings/InlineStrings.jl development by creating an account on GitHub.

> **[GitHub - mkitti/StaticStrings.jl: Fixed-length strings in Julia represented...](https://github.com/mkitti/StaticStrings.jl)**
>
> Fixed-length strings in Julia represented by NTuples - GitHub - mkitti/StaticStrings.jl: Fixed-length strings in Julia represented by NTuples

---

<div class="post-metadata">

**Author:** ![phantomcraft](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@phantomcraft](https://discourse.julialang.org/u/phantomcraft)\
**Post date:** [November 8, 2022, 6:41pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/14 "2022-11-08T18:41:58Z")

</div>

[mkitti](https://discourse.julialang.org/u/mkitti)

> Thanks, I bookmarked all of them.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 8, 2022, 6:57pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/15 "2022-11-08T18:57:38Z")

</div>

> [@Sukera](#):
>
> While disabling GC is an option, writing your code in a way that doesn’t require GC to run much (if at all) by reducing allocations in the first place, is as good as managing memory manually with `malloc` et al.

Data structures often allocate without user control, so this may be hard to achieve in many cases. For instance, in Julia I cannot fill a Dict using a preallocated backing buffer; other languages make this easier.

Another example is that `view`s can allocate without user approval and hurt performance [[RFC/ANN] Restacker.jl: A workaround for the heap-allocated-immutable problem?](https://discourse.julialang.org/t/rfc-ann-restacker-jl-a-workaround-for-the-heap-allocated-immutable-problem/35037).

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 8, 2022, 8:03pm UTC](https://discourse.julialang.org/t/is-it-possible-to-manually-manage-the-memory-without-having-to-use-c-standard-library/89881/16 "2022-11-08T20:03:21Z")

</div>

SimpleChains.jl is a library that manually manages its memory internally via reinterpreting and bumping pointers.  
It will reuse the same memory between calls to avoid allocations.  
Rather than use `malloc`, it just allocates a regular Julia `Vector{UInt8}` and gets the pointer.
