# A nice explanation of memory stack vs. heap

**URL:** https://discourse.julialang.org/t/a-nice-explanation-of-memory-stack-vs-heap/53915
**Category:** Offtopic
**Tags:** memory-allocation
**Created:** [January 25, 2021, 2:39pm UTC](https://discourse.julialang.org/t/a-nice-explanation-of-memory-stack-vs-heap/53915 "2021-01-25T14:39:17Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 25, 2021, 11:36pm UTC](https://discourse.julialang.org/t/a-nice-explanation-of-memory-stack-vs-heap/53915/4 "2021-01-25T23:36:36Z")

</div>

Importantly, the compiler is free to change this in either direction if it can prove that it can safely (unobservably) do so. For example, if it knows that a mutable structure doesn’t outlive the current function then it can stack allocate it. Or in the other direction, if an immutable structure is large and it would be better to heap allocate it than to pass it around on the stack then it can also do that.

---

_[View the full topic](https://discourse.julialang.org/t/a-nice-explanation-of-memory-stack-vs-heap/53915)._
