So, if non needed objects are left referenced
(in a global or static variable,
or as a field of a long-living object),
the garbage collector can’t recognize them,
the memory allocation grows progressively,
and the app eventually crashes with an out-of-memory error.

Why closures require extra attention

One hard-to-catch leak pattern relates to using closures.
In the following code, a reference to the
designed-to-be short-living myHugeObject is implicitly
stored in the closure context and passed to setHandler.
As a result, myHugeObject won’t be garbage collected
as long as handler is reachable.

final

handler

()

print

myHugeObject

name

);

setHandler

handler

);

Why BuildContext requires extra attention