text
stringlengths
1
474
library, method name, or UserTag.<topic_end>
<topic_start>
Guidelines
When looking at a call tree or bottom up view,
sometimes the trees can be very deep.
To help with viewing parent-child relationships in a deep tree,
enable the Display guidelines option.
This adds vertical guidelines between parent and child in the tree.<topic_end>
<topic_start>
Other resources
To learn how to use DevTools to analyze
the CPU usage of a compute-intensive Mandelbrot app,
check out a guided CPU Profiler View tutorial.
Also, learn how to analyze CPU usage when the app
uses isolates for parallel computing.
<topic_end>
<topic_start>Using the Memory view
The memory view provides insights into details
of the application’s memory allocation and
tools to detect and debug specific issues.info Note
This page is up to date for DevTools 2.23.0.For information on how to locate DevTools screens in different IDEs,
check out the DevTools overview.To better understand the insights found on this page,
the first section explains how Dart manages memory.
If you already understand Dart’s memory management,
you can skip to the Memory view guide.<topic_end>
<topic_start>
Reasons to use the memory view
Use the memory view for preemptive memory optimization or when
your application experiences one of the following conditions:<topic_end>
<topic_start>
Basic memory concepts
Dart objects created using a class constructor
(for example, by using MyClass()) live in a
portion of memory called the heap. The memory
in the heap is managed by the Dart VM (virtual machine).
The Dart VM allocates memory for the object at the moment of the object creation,
and releases (or deallocates) the memory when the object
is no longer used (see Dart garbage collection).<topic_end>
<topic_start>
Object types
<topic_end>
<topic_start>Disposable object
A disposable object is any Dart object that defines a dispose() method.
To avoid memory leaks, invoke dispose when the object isn’t needed anymore.<topic_end>
<topic_start>Memory-risky object
A memory-risky object is an object that might cause a memory leak,
if it is not disposed properly or disposed but not GCed.<topic_end>
<topic_start>
Root object, retaining path, and reachability
<topic_end>
<topic_start>Root object
Every Dart application creates a root object that references,
directly or indirectly, all other objects the application allocates.<topic_end>
<topic_start>Reachability
If, at some moment of the application run,
the root object stops referencing an allocated object,
the object becomes unreachable,
which is a signal for the garbage collector (GC)
to deallocate the object’s memory.<topic_end>
<topic_start>Retaining path
The sequence of references from root to an object
is called the object’s retaining path,
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>