Memory management in Julia



I’m new in Julia and I got a doubt about how the memory management and allocation works, please be patient. In the code below, the allocated memory space for the variable “x” it’s on heap, stack or base? And the size of this space (generally in bytes) is predefined by the Julia compiler (as JAVA does) or it depends on the architeture of the machine where the program is being executed (just like C does)?

julia> x = "45*2= $(45*2)"
"45*2= 90"


First just to clearify no there’s no variable x being allocated. In julia variable does NOT equal to objects. All what variable (global or local) can do are referring to objects.

Because of/related to this, all objects that you are allowed to take address of are semantically allocated on the heap. You are not allowed to take address of isbits objects so semantically it doesn’t make sense to talk about where they are allocated (because they are never allocated in any visible way).

As long as the semantic above is maintained, the runtime/compilier can do whatever optimization it wants. It is allowed to stack allocate anything as long as it can proof the user won’t notice. It can remove the construction/allocation of an object altogether if there’s no use of it. This obviously requires a definition for what’s observable and one of them is that global bindings escapes an objects so any !isbits values (objects) you stored to global variable will be allocated on the heap.

In the case you have above, the string will be allocated on the heap because,

  1. String is never isbits.
  2. All objects binded to global variables are allocated on the heap even though nothing requires isbits values to do that.

P.S. please quote your code.