Skip to main content

Memory management

You never free memory yourself in Nylon. Every instance lives on its isolate's heap, and the VM reclaims it once nothing can reach it.

Allocating​

Making an instance, such as Person("Ada"), takes its memory from the heap. Only the isolate's own work allocates from its heap, and nothing else does. On the board an instance stays where it was made: nothing moves it.

Reclaiming​

Memory comes back in two ways, which work together.

  • Reference counting frees an instance the moment the last reference to it goes. Most memory comes back this way, straight away.
  • A collector finds what counting can't. Two instances that refer to each other keep each other's count above zero, even when nothing else can reach either of them. The collector traces from what the program can still reach, and frees the rest.

The collector runs when the heap has grown: once the program has allocated about as much again as was in use when its last pass ended.

What a running program notices​

The collector works alongside your program rather than stopping it. Your program pauses only briefly:

  • While the collector reads its stack. The pause depends on how deep the program's calls are at that moment, not on how much memory it uses.
  • At each step of a pass. Running work stops for a moment at its next safe point.

While a pass is marking, storing into a var field that holds an instance does a little extra work, so the collector doesn't miss it.

No timing promise in V0

V0 doesn't promise how long a pause lasts, or when one comes. Don't build anything that must happen at an exact moment, such as a flight controller's control loop, on V0's timing. Real-time guarantees come in a later version.

When memory runs out​

A program that needs more memory than its heap can have stops with a fault, which the board reports as HeapAtLimit. The program doesn't wait for memory to come back. The heap can have as much memory as the board gives it.

What V0 doesn't have​

  • No way to free memory yourself, and no way to start or tune the collector.
  • No finalizers: code that runs when an instance is reclaimed.
  • No weak references: a reference that doesn't keep its target alive.