# 1.0 multi-threading memory use issues

**URL:** <https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454>\
**Category:** Crystal Contrib\
**Created:** [August 18, 2020, 6:22pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454 "2020-08-18T18:22:32Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 22, 2020, 6:41pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/21 "2020-08-22T18:41:16Z")

</div>

Hey @asterite, after studying your code, I just replaced the `Arrays` with `Slices` in the two methods `twins_sieve` and `nextp_init` in my original version, as you did in your code, and here are the results, for input of 1\_000\_000\_000\_000.

My original using Arrays: 174.5 secs  
My original using Slices: 161.1 secs

The memory still grows as before, but that’s seem to be due to, as your stated, the incomplete multi-threading model.

I want to say I really appreciate how you jumped on this issue and made the code much better. I have no doubt Crystal will catch up to Nim, Rust, et al, in multi-processing very soon.  
Please feel free to use this application as you please as a test case.  
Here is the updated gist of my code using `Slices`.

> <https://gist.github.com/jzakiya/2b65b609f091dcbb6f792f16c63a8ac4>

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [August 22, 2020, 9:53pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/22 "2020-08-22T21:53:44Z")

</div>

Cool!

Yeah, I love optimizing things.

It strange that the memory keeps goigg up for you. In my case it never goes beyond 20MB. Maybe a difference of OS? No idea.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [August 22, 2020, 10:54pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/23 "2020-08-22T22:54:49Z")

</div>

> [@asterite](#):
>
> And no, there will never be manual memory management in Crystal.

There already is, one can call `LibC.malloc` as easily as any other C function.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 22, 2020, 11:01pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/24 "2020-08-22T23:01:24Z")

</div>

> [@asterite](#):
>
> It strange that the memory keeps goigg up for you. In my case it never goes beyond 20MB. Maybe a difference of OS? No idea

My laptop for these results is a 2011 Lenovo, I5 2C|4T, 2.9 GHz, Linux kernel 5.7.15.  
Crystal 0.35.1

---

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [August 24, 2020, 8:20pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/25 "2020-08-24T20:20:52Z")

</div>

Actually, I want to experiment with that flag as part of my old idea to use ARC (automatic Reference Counting) as possible option to GC.

---

<div class="post-metadata">

**Author:** ![giuse](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/giuse/32/600_2.png) [@giuse](https://forum.crystal-lang.org/u/giuse)\
**Post date:** [August 25, 2020, 12:58pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/26 "2020-08-25T12:58:56Z")

</div>

And how do you use in crystal? I’ve got no idea and I thought it would be impossible due to the strong type system

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [August 26, 2020, 5:34pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/27 "2020-08-26T17:34:55Z")

</div>

Yes, to be absolutely clear, doing so is **inherently unsafe** and can _easily_ lead to memory leaks! `LibC.malloc` returns a raw [`Pointer`](https://crystal-lang.org/api/0.35.1/Pointer.html), which you can then wrap in a slice and pass around like any other, but doing so discards guarantees the compiler offers (memory safety).

---

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [August 27, 2020, 9:51pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/28 "2020-08-27T21:51:12Z")

</div>

That’s where ARC should help.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [August 27, 2020, 10:03pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/29 "2020-08-27T22:03:12Z")

</div>

ARC would be a dream come true, for sure! I just don’t feel capable of implementing it personally.

---

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [August 27, 2020, 11:31pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/30 "2020-08-27T23:31:48Z")

</div>

I am planning to do it but don’t have enough time. It may be equal or even may be more work than debugger support (I spent 6 months on it).  
But I am still willing to work on it as it is an interesting idea to implement.

---

<div class="post-metadata">

**Author:** ![megatux](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/megatux/32/164_2.png) [@megatux](https://forum.crystal-lang.org/u/megatux)\
**Post date:** [August 28, 2020, 4:46pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/31 "2020-08-28T16:46:51Z")

</div>

You mean ARC like the ones implemented in Swift & ObjectiveC with the LLVM support? ( **retain** and **release** ). That would be awesome, I think.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 28, 2020, 6:46pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/32 "2020-08-28T18:46:40Z")

</div>

You may want to follow the development of Nim’s ARC.

[https://forum.nim-lang.org/t/5734](https://forum.nim-lang.org/t/5734)

> **[NimConf 2020](https://conf.nim-lang.org/)**
>
> NimConf 2020 will take place on June 20th at 11am UTC. Streamed live and for free from YouTube.

---

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [August 29, 2020, 10:37pm UTC](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454/33 "2020-08-29T22:37:13Z")

</div>

Yes, exactly that approach.

[Previous page](https://forum.crystal-lang.org/t/1-0-multi-threading-memory-use-issues/2454.md?page=1)
