Memory allocations

Hi there, I was wondering why the line @allocated A = 100 gives 0, whereas @allocated B = [1,2,3,4,5] gives 96. Both can result in a completely fresh REPL, so why does defining A = 100 not use any memory? Surely the information of the value that A has been assigned needs to be stored somewhere?

Hi Lucas!
The number returned by @allocated actually corresponds to the number of bytes allocated on the “heap”, as opposed to the “stack”. Roughly speaking, Julia allocates immutable objects (like integers) on the stack, which is very fast, but mutable objects (like vectors) on the heap, which is much slower.
When we want to optimize the performance of a code, usually we seek to reduce heap allocations, and that is why @allocated focuses on those.

9 Likes

I didn’t know that - that clears things up, thank you!

1 Like