# How to reduce memory usage in the Crystal language?

**URL:** https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340
**Category:** Help & Support
**Created:** [January 19, 2024, 2:53am UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340 "2024-01-19T02:53:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)
#### Post date: [January 19, 2024, 2:53am UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340/1 "2024-01-19T02:53:14Z")

</div>

Hello.

I really like the performance of Crystal.

I recently used [Valgrind](https://valgrind.org/) and [Hotspot](https://github.com/KDAB/hotspot) on Linux and [Instruments](https://en.wikipedia.org/wiki/Instruments_(software)) on macOS to investigate performance bottlenecks and learned that I can speed up the program.

I am not quite sure how to reduce memory usage though.

Do you have any recommendations?

For example, it would be great if there was a way to visualize how instances of Crystal objects are occupying memory over time during program execution in the form of [Treemapping](https://en.wikipedia.org/wiki/Treemapping)…

The screenshot below is of a software called [GrandPerspective](https://grandperspectiv.sourceforge.net/) for macOS.

> **GrandPerspective** is a small utility application for macOS that graphically shows the disk usage within a file system.

 ![3_4_1-Creation_Heathmap12](https://canada1.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/a/adb20c8b7e4ad41f20b98f043ccb6a4d57403f4c.png)

I wish I could do this kind of thing not only with HDD but also with memory, although such a useful thing may not exist.

---

<div class="post-metadata">

### Author: ![hugopl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hugopl/32/970_2.png) [@hugopl](https://forum.crystal-lang.org/u/hugopl)
#### Post date: [January 19, 2024, 2:04pm UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340/2 "2024-01-19T14:04:19Z")

</div>

> [@kojix2](#):
>
> I am not quite sure how to reduce memory usage though.

Crystal GC uses sbrk to allocate memory and valgrind doesn’t support applications that allocate memory that way ☹

But IIRC people already posted in this forum other workarounds to track memory usage by monkey patching some classes in stdlib.

---

<div class="post-metadata">

### Author: ![beta-ziliani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/beta-ziliani/32/1821_2.png) [@beta-ziliani](https://forum.crystal-lang.org/u/beta-ziliani)
#### Post date: [January 19, 2024, 2:20pm UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340/3 "2024-01-19T14:20:08Z")

</div>

Maybe you are referring to [perf-tools](https://github.com/crystal-lang/perf-tools)? in any case, it won’t give any visual map like the one asked here. But @kojix2 if you find a good visualizer to do that, maybe you can tweak the output of `perf_tools/mem_prof` to make it work.

---

<div class="post-metadata">

### Author: ![drmidnight](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drmidnight/32/2266_2.png) [@drmidnight](https://forum.crystal-lang.org/u/drmidnight)
#### Post date: [January 19, 2024, 3:37pm UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340/4 "2024-01-19T15:37:17Z")

</div>

You can also use Instruments to track allocations. It doesn’t have a nice view like GP and you can’t really tell what the object was, but it can quickly point you towards issues.

---

<div class="post-metadata">

### Author: ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)
#### Post date: [January 19, 2024, 5:39pm UTC](https://forum.crystal-lang.org/t/how-to-reduce-memory-usage-in-the-crystal-language/6340/5 "2024-01-19T17:39:27Z")

</div>

Valgrind includes separate tools to track allocations and stuff. Unfortunately it doesn’t work out of the box with libgc, but perhaps it is possible to make it work somehow. At the very least it should work with GC::None, but that is more than a little bit limiting, of course.

But if someone could figure out how to make them work together by default then that would of course be optimal.
