# How can I pre-allocate a dict?

**URL:** <https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000>\
**Category:** General Usage\
**Created:** [April 3, 2023, 6:36am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000 "2023-04-03T06:36:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)\
**Post date:** [April 3, 2023, 6:36am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/1 "2023-04-03T06:36:14Z")

</div>

I have a function 1, contain allocation of some small dict, about two dict with less than 10 elements. And I have function 2 call this function 1 in a for loop about 2^12 times. Then I have call function 2 in a for loop for 2^10 times. Then I found my memory increased quickly, but actually I never remember these small dicts.  
I tried GC.gc() after each loop, the memory will decrease a lot but still much bigger than my expect.  
I don;t know if it’s the small dict couse huge memory.  
I want to pre-allocate a dict, so I can use it each time rather than re-allocate a new one. But I have not found a method to pre-allocate a given size dict.  
Help! Thanks in advance!

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [April 3, 2023, 7:11am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/2 "2023-04-03T07:11:39Z")

</div>

> [@Can we reuse Dictionary allocation?](https://discourse.julialang.org/t/can-we-reuse-dictionary-allocation/85582/2):
>
> I think you could just empty! it at the end of each loop iteration. My understanding is that it won’t shrink the internal vectors. In fact, there’s no way to shrink the internal vectors until julia 1.9, when sizehint! will be able to shrink them, thanks to [https://github.com/JuliaLang/julia/pull/45004](https://github.com/JuliaLang/julia/pull/45004).

---

<div class="post-metadata">

**Author:** ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)\
**Post date:** [April 3, 2023, 9:42am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/3 "2023-04-03T09:42:04Z")

</div>

In function 1, first I create a new dict with Dict(), then I append some key-value pairs. How can I use empty! to avoid allocation?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 3, 2023, 9:57am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/4 "2023-04-03T09:57:29Z")

</div>

If your dict is small, consider using an alternative implmentation, the [`ArrayDictionary`](https://github.com/andyferris/Dictionaries.jl) may be an option?

---

<div class="post-metadata">

**Author:** ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)\
**Post date:** [April 4, 2023, 2:16am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/5 "2023-04-04T02:16:05Z")

</div>

Thank you, I will try it.

---

<div class="post-metadata">

**Author:** ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)\
**Post date:** [April 4, 2023, 3:23am UTC](https://discourse.julialang.org/t/how-can-i-pre-allocate-a-dict/97000/6 "2023-04-04T03:23:16Z")

</div>

But the biggest problem is my program will be killed because it used lots of memory, about 300G. I don’t why, I didn’t need so much memory to store the needed variables.  
I frequently allocate small dict, but I use empty! to avoid re-allocate dict, the memory is still increased quickly.  
And I also used a structure of dict’s dict to store sth. I need. I will add some (key, dict) to the dict and read out the (key, dict) pairs throughout my main function. Someone told me that such dict’s dict structure may cause memory leak, but he don’t remember where he saw it, I don’t know if it’s true.  
I estimate that the memory I need is about 40G, but the program used up to 300G.
