Memory

Luma frees memory by itself, at a moment you can predict: as soon as nothing uses a value anymore. There is no garbage collector running in the background and no pauses. When you are done with a variable earlier than its block ends, free ends it on the spot.

When a value is freed

Strings, lists, maps, sets, errors and objects take memory. Luma counts how many places hold each one: variables, elements of lists and maps, function parameters. When the count drops to zero, the memory is freed right away. That happens when:

  • a variable’s block ends. A variable declared inside an if, a walk or a function is gone at its closing }. A variable declared inside a walk is freed at the end of every step.
  • a variable gets a new value. The old value is freed, unless something else still holds it.
  • an element is replaced or removed from a list or map.
  • a value was only needed for a moment, like the string built by "item ${n}" to be printed.

So a loop can run for as long as it likes. Memory stays the same as long as the loop doesn’t keep its values:

total: int = 0
[1, 2, 3, 4, 5].walk(n) {
    words: [str] = ["item ${n}", "of 5"]   // a new list each step...
    total = total + len(words)
}                                          // ...freed at the end of each step
print(total)

Output:

10

Shared values stay as long as they are used

Lists, maps, sets and objects are shared on assignment and when passed to functions. A shared value is freed only when the last holder lets go, so a value can never disappear while it is still in use:

fn make(): [str] {
    list: [str] = ["made inside"]
    return list               // the function's variable is gone, the list lives on
}

a: [str] = make()
b: [str] = a                  // shared: two holders
a = ["something else"]        // one holder left: the list stays
print(b)

Output:

["made inside"]

Strings, lists, maps and sets can’t form a circle (a list can’t contain itself), so they are always freed in the end. Only objects can refer to each other in a circle, and those circles are not freed (see Limits).

free: I’m done with it

free(x) ends the variable x right there, as if its block had ended. Its value is freed (if nothing else holds it), and x is gone: using it afterwards is a compile error.

lines: [str] = ["a long list", "read from a file", "..."]
count: int = len(lines)
free(lines)          // the list is freed now, not when the program ends
print(count)

Output:

3
print(lines)         // error: undefined variable lines (it was freed at 3:1)

This matters most for big values in variables that live long: a top-level variable otherwise lasts until the program ends, and a function’s variable until the function returns.

The name can be declared again after free, even with another type:

data: [int] = [1, 2, 3]
free(data)
data: str = "something new"
print(data)

Output:

something new

Rules:

  • free(x) must be in the same block where x was declared. A variable can’t be freed inside an if or a walk of its block: there it would be gone on one way through the program but not on the other. (A function’s parameters can be freed in the function’s own block.)
  • A walk’s variable can’t be freed: it ends by itself with each step.
  • A shared value lives on for its other holders. After b: [int] = a and free(a), a is gone, but the list stays as long as b (or a list or map holding it) uses it. So free can never leave anything pointing at freed memory.
  • Any variable can be freed. For a number, free just ends the variable: numbers don’t take memory of their own.
  • free is for variables. To take an element out of a list or map, use remove.
nums: [int] = [1, 2, 3]
if len(nums) > 2 {
    free(nums)       // error: free nums in the block where it was declared (at 1:1)
}
free(nums[0])        // error: free ends a variable; to take an element out ... use remove(nums, ...)

Checking a program’s memory

luma build -memcheck makes an executable that checks its own memory. It is slower, and meant for testing:

  • Memory is overwritten as soon as it is freed, so if the compiler ever freed a value that is still in use, the output would show garbage.
  • At the end, the program reports values that were never freed:
luma: 3 objects were not freed

Luma’s own tests build every test program this way.

The interpreter (luma run) uses Go’s memory management, and the output is the same either way.

Limits

  • Errors free what they leave behind. When an error is raised, everything it jumps over is freed: the variables of the functions it leaves, those declared inside the try, the values half-way through the statement that failed (the list in print("a,b".split(",")[7])), and what a built-in was making when it failed (half a JSON document). A program can raise and catch errors in a loop forever without using more memory.
  • Objects that refer to each other in a circle are never freed. Counting can’t tell that only the circle itself still holds them: a node put into its own children list, or two objects in each other’s lists, stay in memory until the program ends (-memcheck reports them). Lists, maps and sets alone can’t form circles; only objects can. Break the circle before letting go, for example node.children = [].
  • Freed memory is reused by the program, not returned to the operating system while the program runs. A program’s memory use is the most it needed at any one time.

See Limitations.