Skip to content
Plumlang

Language reference · Section 11

Values and memory

11.1 Value types and reference types

Numbers, booleans, none, strings, lists, maps, sets and kinds are values: assigning or passing one gives the receiver its own copy, so changing one never changes the other.

plant a = [1, 2, 3]
plant b = a
b.push(4)
drop(a)        # [1, 2, 3]
drop(b)        # [1, 2, 3, 4]

Copies are cheap: the data is shared until one side changes it, and only then copied (copy-on-write).

Tree objects are references: assigning or passing one shares the object (section 8.3).

11.2 Memory is reclaimed automatically and at once

Every value is freed as soon as nothing refers to it anymore, by reference counting. There is no garbage collector, so there are no pauses, and memory use follows the program. Files and sockets close when their last reference goes away, as well as when close() is called.

11.3 Cycles and weak references

Two tree objects that refer to each other keep each other alive. Break the cycle by making the back-reference weak; a weak reference does not keep its object alive and reads as none once the object is gone, so its type is optional:

tree Node {
    children: list[Node] = []
    parent: weak Node? = none
}

weak goes on a field whose type is an optional tree or trait (E0425). Assigning an object to a weak field doesn't keep it alive; reading the field gives the object while something else holds it, and none after.

plum run --check-leaks program.plum reports, when the program ends, the tree objects still alive because they are in a cycle, by tree and the line that made them, and exits with code 1 if there are any:

leak check: 2 objects are still alive in reference cycles:
  1 Pair, made on line 29
  1 Pair, made on line 30
  hint: make one direction of each cycle weak, like 'parent: weak Node? = none'

11.4 What the runtime guarantees

No Plumlang program can read or write memory it does not own: every index is checked, there are no raw pointers, and objects are never used after they are freed. A str holds at most 1 GiB (2^30 bytes) and a list at most 2^27 (134,217,728) items; making a larger one (with +, repeat, join, push...) throws a ValueError before any memory is taken. A program that runs out of memory otherwise ends with plum: error: out of memory and exit code 1. Values may nest as deep as memory allows: comparing, copying and freeing them doesn't recurse in the runtime, and printing stops at a depth (section 3.2.1).

11.5 Measuring memory

plum run --mem-stats program.plum prints to standard error, when the program ends, how much memory the run used:

memory used by this run (--mem-stats):
  peak in use          82,456 bytes (80.5 KB)
  allocated in all     167,008 bytes (163.1 KB)
  still in use at exit 0 bytes
  allocations          3,014
  resizes              12
  frees                3,014
values made:
  strings              2,001
  lists                1
  maps                 0
  ...

Bytes are the sizes of the blocks the C library hands out, counted from the start of the run to its end (compiling comes before and isn't counted). "Still in use at exit" is what the run allocated and didn't free: more than 0 means objects left alive in reference cycles (find them with --check-leaks). Values made are counted by kind: strings, lists, maps, sets, tree objects, kind values, closures, captured variables, weak references, tasks and channels. Without --mem-stats nothing is counted and the run costs the same.