text
stringlengths 1
372
|
|---|
as it retains the object’s memory from the garbage collection.
|
one object can have many retaining paths.
|
objects with at least one retaining path are
|
called reachable objects.
|
<topic_end>
|
<topic_start>
|
example
|
the following example illustrates the concepts:
|
<topic_end>
|
<topic_start>
|
shallow size vs retained size
|
shallow size includes only the size of the object
|
and its references, while retained size also includes
|
the size of the retained objects.
|
the retained size of the root object includes
|
all reachable dart objects.
|
in the following example, the size of myHugeInstance
|
isn’t part of the parent’s or child’s shallow sizes,
|
but is part of their retained sizes:
|
in DevTools calculations, if an object has more
|
than one retaining path, its size is assigned as
|
retained only to the members of the shortest retaining path.
|
in this example the object x has two retaining paths:
|
only members of the shortest path (d and e) will include
|
x into their retaining size.
|
<topic_end>
|
<topic_start>
|
memory leaks happen in dart?
|
garbage collector cannot prevent all types of memory leaks, and developers
|
still need to watch objects to have leak-free lifecycle.
|
<topic_end>
|
<topic_start>
|
why can’t the garbage collector prevent all leaks?
|
while the garbage collector takes care of all
|
unreachable objects, it’s the responsibility
|
of the application to ensure that unneeded objects
|
are no longer reachable (referenced from the root).
|
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.
|
<topic_end>
|
<topic_start>
|
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.
|
<topic_end>
|
<topic_start>
|
why BuildContext requires extra attention
|
an example of a large, short-living object that
|
might squeeze into a long-living area and thus cause leaks,
|
is the context parameter passed to flutter’s
|
build method.
|
the following code is leak prone,
|
as useHandler might store the handler
|
in a long-living area:
|
<topic_end>
|
<topic_start>
|
how to fix leak prone code?
|
the following code is not leak prone,
|
because:
|
<topic_end>
|
<topic_start>
|
general rule for BuildContext
|
in general, use the following rule for a
|
BuildContext: if the closure doesn’t outlive
|
the widget, it’s ok to pass the context to the closure.
|
stateful widgets require extra attention.
|
they consist of two classes: the widget and the
|
widget state,
|
where the widget is short living,
|
and the state is long living. the build context,
|
owned by the widget, should never be referenced
|
from the state’s fields, as the state won’t be garbage
|
collected together with the widget, and can significantly outlive it.
|
<topic_end>
|
<topic_start>
|
memory leak vs memory bloat
|
in a memory leak, an application progressively uses memory,
|
for example, by repeatedly creating a listener,
|
but not disposing it.
|
memory bloat uses more memory than is necessary for
|
optimal performance, for example, by using overly large
|
images or keeping streams open through their lifetime.
|
both leaks and bloats, when large,
|
cause an application to crash with an out-of-memory error.
|
however, leaks are more likely to cause memory issues,
|
because even a small leak,
|
if repeated many times, leads to a crash.
|
<topic_end>
|
<topic_start>
|
memory view guide
|
the DevTools memory view helps you investigate
|
memory allocations (both in the heap and external),
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.