# Memory management in Julia

**URL:** https://discourse.julialang.org/t/memory-management-in-julia/6360
**Category:** New to Julia
**Tags:** memory-allocation
**Created:** [October 9, 2017, 6:50pm UTC](https://discourse.julialang.org/t/memory-management-in-julia/6360 "2017-10-09T18:50:19Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jwesley](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jwesley](https://discourse.julialang.org/u/jwesley)
#### Post date: [October 9, 2017, 6:50pm UTC](https://discourse.julialang.org/t/memory-management-in-julia/6360/1 "2017-10-09T18:50:19Z")

</div>

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
julia> x = "45*2= $(45*2)"
"45*2= 90"

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 9, 2017, 9:07pm UTC](https://discourse.julialang.org/t/memory-management-in-julia/6360/2 "2017-10-09T21:07:15Z")

</div>

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.
