# Preallocating Array in a Memory Efficient Way

**URL:** <https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206>\
**Category:** New to Julia\
**Tags:** array, memory, memory-allocation\
**Created:** [December 12, 2019, 9:57pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206 "2019-12-12T21:57:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [December 12, 2019, 9:57pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/1 "2019-12-12T21:57:45Z")

</div>

Hi,

I need to work with a very big Array

`z=zeros(Int64,2^K,2^K,K,T)`

What is the proper way of pre-allocating this Array in such a way that it uses the least memory possible when K is large?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 12, 2019, 10:32pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/2 "2019-12-12T22:32:31Z")

</div>

How big is `K`? Will most of the elements be `0`? If so, using a sparse data structure will be helpful. Also, are you sure you need to store the array? If you can get the same effect from looping over a small portion of it, that should be much more efficient.

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [December 13, 2019, 2:08pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/3 "2019-12-13T14:08:57Z")

</div>

K is around 15! Yes, most elements will be 0. However, most of this are 3-dimensional arrays and I could not get sparse structures to work in this case. A MWE of my problem can be found here [Performance Tips for Combinatorial Problem](https://discourse.julialang.org/t/performance-tips-for-combinatorial-problem/32208)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 13, 2019, 2:11pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/4 "2019-12-13T14:11:00Z")

</div>

Is there any structure to it? Blocks, for instance?

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [December 13, 2019, 2:14pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/5 "2019-12-13T14:14:00Z")

</div>

Yes, you can think of `z=zeros(Int64,2^K,2^K,K,T)` as 2^K_2^K matrices of size K_T

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 13, 2019, 2:32pm UTC](https://discourse.julialang.org/t/preallocating-array-in-a-memory-efficient-way/32206/6 "2019-12-13T14:32:14Z")

</div>

If you don’t need all of the matrices at the same time, store them as an array of arrays, with possibly “nothing” entries.
