# How does SVector manage memory?

**URL:** <https://discourse.julialang.org/t/how-does-svector-manage-memory/32376>\
**Category:** New to Julia\
**Created:** [December 17, 2019, 11:47am UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376 "2019-12-17T11:47:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [December 17, 2019, 11:47am UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/1 "2019-12-17T11:47:26Z")

</div>

```julia
julia> using StaticArrays

julia> using BenchmarkTools

julia> x = [1, 2, 3]
julia> y = SVector{3, Int64}(x)

julia> @btime exp.($x);
  62.574 ns (1 allocation: 112 bytes)
julia> @btime exp.($y);
  0.022 ns (0 allocations: 0 bytes)

```

showing `SVector` uses **zero** memory. As far as I understand, `SVector` uses “stack memory” while `Vector` uses “heap memory”. But they’re both memories (i.e. using RAM), how could `@btime` report 0 bytes?!  
also, I read that using “stack memory” could “avoid the expensive garbage collection”. These rise the following questions:

1. why using stack memory is faster than heap memory (apart from the time in gc)?
2. why `@btime` reports fewer (or even zero) memory usage when stack memory is used?
3. does stack memory have any mechanism for garbage collection? if yes, could we control the gc?
4. if 3. is no, does it imply that creating (even small) `SVector` over millions of iterations would inevitably run out of the stack memory?

many thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 17, 2019, 11:56am UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/2 "2019-12-17T11:56:59Z")

</div>

You can find general (not Julia-specific) descriptions of stacks to answer all of your questions, eg

> **[Stack-based memory allocation](https://en.wikipedia.org/wiki/Stack-based_memory_allocation)**
>
> Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out (LIFO) manner.
> In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its local state data to the top of the stack; when the function exits it is responsible for removing that data from the stack. At a minimum, a thread's stack is used to store the location of a return address provided by the ca...

GC does not apply (conceptually). Also, for LLVM, the actual objects may just be in registers etc when small, not in RAM.

As for (4), it depends on _how_ you get those million objects on the stack. Eg if you are recursively calling functions that do this, you may experience problems. This is not what the stack is for.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [December 17, 2019, 2:51pm UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/3 "2019-12-17T14:51:44Z")

</div>

> [@Tamas\_Papp](#):
>
> if you are recursively calling functions that do this, you may experience problems.

fortunately it’s not what I do.

By the way, whenever running out of stack memory, what would `StaticArrays` (or `Julia`) do? Would they be smart enough to switch to heap memory?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 17, 2019, 3:02pm UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/4 "2019-12-17T15:02:35Z")

</div>

You get a [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow).

You should design your algorithms so that this does not happen.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [December 17, 2019, 3:08pm UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/5 "2019-12-17T15:08:06Z")

</div>

according to the wiki you posted above:

> memory on the stack is automatically, and very efficiently, reclaimed when the function exits

is it also true for `StaticArrays` or `Julia` in general? I’m just a bit uncomfortable switching to `SVector` as I have no idea how large the “stack memory” is and I have no control over “gc”…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 17, 2019, 3:31pm UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/6 "2019-12-17T15:31:15Z")

</div>

Again, this is only relevant if you are doing very deep recursions (or, possibly, your static vectors are huge, but then you should not use them anyway). This should not be a concern for idiomatic Julia code, with or without static vectors.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [December 17, 2019, 3:31pm UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/7 "2019-12-17T15:31:45Z")

</div>

@tomtom: let me give you two answers and you choose which one applies best to your situation:

1. Practical answer: Yes, Julia(/LLVM) is smart enough to allocate largish structures on the heap instead of the stack. The exact threshold is an implementation detail (it may even keep it in registers, etc). So as a language user, you don’t really have to worry about it. Just use `SVector`s and measure if you are getting better timings.

2. Theoretical answer: if you want to know exactly what’s going on, you’d have to start with actually understanding registers, stack, heap, and calling conventions. If that’s what you want, Julia is not the best place to start: it’s actually better to study the `C` language for a bit, and see how it manages memory. If that’s what you want, [this one looks pretty good (bit I haven’t read it fully)](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-introduction-to-c-and-c-january-iap-2013/lectures-and-assignments/c-memory-management/).

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [December 18, 2019, 2:26am UTC](https://discourse.julialang.org/t/how-does-svector-manage-memory/32376/8 "2019-12-18T02:26:47Z")

</div>

thanks
