text
stringlengths 1
474
|
|---|
<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),
|
memory leaks, memory bloat, and more. The view
|
has the following features:<topic_end>
|
<topic_start>
|
Expandable chart
|
The expandable chart provides the following features:<topic_end>
|
<topic_start>Memory anatomy
|
A timeseries graph visualizes the state of
|
Flutter memory at successive intervals of time.
|
Each data point on the chart corresponds to the
|
timestamp (x-axis) of measured quantities (y-axis)
|
of the heap. For example, usage, capacity, external,
|
garbage collection, and resident set size are captured.<topic_end>
|
<topic_start>Memory overview chart
|
The memory overview chart is a timeseries graph
|
of collected memory statistics. It visually presents
|
the state of the Dart or Flutter heap and Dart’s
|
or Flutter’s native memory over time.The chart’s x-axis is a timeline of events (timeseries).
|
The data plotted in the y-axis all has a timestamp of
|
when the data was collected. In other words,
|
it shows the polled state (capacity, used, external,
|
RSS (resident set size), and GC (garbage collection))
|
of the memory every 500 ms. This helps provide a live
|
appearance on the state of the memory as the application is running.Clicking the Legend button displays the
|
collected measurements, symbols, and colors
|
used to display the data.The Memory Size Scale y-axis automatically
|
adjusts to the range of data collected in the
|
current visible chart range.The quantities plotted on the y-axis are as follows:<topic_end>
|
<topic_start>
|
Profile Memory tab
|
Use the Profile Memory tab to see current memory
|
allocation by class and memory type. For a
|
deeper analysis in Google Sheets or other tools,
|
download the data in CSV format.
|
Toggle Refresh on GC, to see allocation in real time.<topic_end>
|
<topic_start>
|
Diff Snapshots tab
|
Use the Diff Snapshots tab to investigate a feature’s
|
memory management. Follow the guidance on the tab
|
to take snapshots before and after interaction
|
with the application, and diff the snapshots:Tap the Filter classes and packages button,
|
to narrow the data:For a deeper analysis in Google Sheets
|
or other tools, download the data in CSV format.<topic_end>
|
<topic_start>
|
Trace Instances tab
|
Use the Trace Instances tab to investigate what methods
|
allocate memory for a set of classes during feature execution:<topic_end>
|
<topic_start>Bottom up vs call tree view
|
Switch between bottom-up and call tree views
|
depending on specifics of your tasks.The call tree view shows the method allocations
|
for each instance. The view is a top-down representation
|
of the call stack, meaning that a method can be expanded
|
to show its callees.The bottom-up view shows the list of different
|
call stacks that have allocated the instances.<topic_end>
|
<topic_start>
|
Other resources
|
For more information, check out the following resources:
|
<topic_end>
|
<topic_start>Using the Debug console
|
The DevTools Debug console allows you to watch an
|
application’s standard output (stdout),
|
evaluate expressions for a paused or running
|
app in debug mode, and analyze inbound and outbound
|
references for objects.info Note
|
This page is up to date for DevTools 2.23.0.The Debug console is available from the Inspector,
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.