Sub topic 2
Isolates: separate heaps, message passing
Assume that you know basic Dart syntax, async and await, Future, and basic streams. For the Flutter examples, also assume that you know why the UI must stay responsive.
An isolate is an independent Dart execution context. It has its own event loop, call stack, and heap. The heap is the area of memory where Dart objects live. A Dart program starts with one isolate, often called the main isolate. Flutter runs its UI work there.
Separate heaps
Each isolate owns its mutable objects. An object created in one isolate cannot be changed directly by another isolate. There is no shared mutable memory between isolates.
This design avoids many thread problems. Two threads that share an object can change it at the same time, which creates race conditions. Isolates do not need locks for ordinary Dart objects because another isolate cannot access those objects directly.
The cost is that isolates cannot call each other's functions or read each other's variables. They communicate by sending messages through ports.
An isolate can still perform many tasks at once over time. Its event loop processes one callback at a time. An async function can pause while waiting for input, but synchronous code still runs without interruption inside that isolate.
If a synchronous callback takes too long, the isolate cannot process other callbacks. In a Flutter app, that can delay frame work and user input. A second isolate gives the expensive computation a different event loop.
Message passing with ports
A ReceivePort receives messages. A SendPort sends messages to its matching receive port. You usually give a new isolate a SendPort when starting it, or send a worker's SendPort back to the main isolate during setup.
Messages are values, not references to the original objects. The receiving isolate gets its own usable representation. Changing a received list does not change the list that the sender originally created.
import 'dart:isolate';
void worker(SendPort mainPort) {
final input = ReceivePort();
mainPort.send(input.sendPort);
input.listen((message) {
final number = message as int;
mainPort.send(number * number);
});
}
Future<void> main() async {
final output = ReceivePort();
await Isolate.spawn(worker, output.sendPort);
final workerPort = await output.first as SendPort;
workerPort.send(12);
final result = await output.first as int;
print(result); // 144
output.close();
}The first message is the worker's SendPort. The main isolate uses it to send 12. The worker receives the number, calculates its square, and sends 144 back.
The message must contain values that Dart can transfer between isolates. Simple values such as numbers, strings, booleans, lists, maps, records, and ports are common choices. The complete object graph must be transferable. A closure can accidentally include objects from its surrounding scope, so it may contain more data than its parameter suggests.
Messages also have a cost. Large structures may need to be copied, and copying takes time and memory. For large byte buffers, TransferableTypedData can transfer ownership more efficiently. The receiving isolate must materialize the data before reading it.
The working model
Think of an isolate as a small program with private memory and an inbox:
- The isolate runs callbacks from its event loop.
- A
SendPortplaces a message in another isolate's inbox. - The receiving isolate handles that message on its own event loop.
- A reply travels back as another message.
This model makes ownership clear. The sender owns its objects, the receiver owns the received values, and communication happens explicitly.
Exercise
Why can two isolates safely update their own lists at the same time, and what must they do if one isolate needs the other list's contents?
Show answer
Each isolate has a separate heap, so neither can directly mutate the other's list. The isolate that needs the contents must receive them through a message, usually as a copied or transferred value.