Skip to content

Create tab 'Leaks' #4211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ class MemoryController extends DisposableController
ValueListenable<DateTime?> get selectedSnapshotNotifier =>
_selectedSnapshotNotifier;

final _shouldShowLeaksTab = ValueNotifier<bool>(false);
ValueListenable<bool> get shouldShowLeaksTab => _shouldShowLeaksTab;

static String formattedTimestamp(DateTime? timestamp) =>
timestamp != null ? DateFormat('MMM dd HH:mm:ss').format(timestamp) : '';

Expand Down Expand Up @@ -815,7 +818,15 @@ class MemoryController extends DisposableController
// TODO(terry): Need an event on the controller for this too?
}

void _handleConnectionStart(ServiceConnectionManager serviceManager) {
void _refreshShouldShowLeaksTab() {
_shouldShowLeaksTab.value = serviceManager.serviceExtensionManager
.hasServiceExtension(memoryLeakTrackingExtensionName)
.value;
}

void _handleConnectionStart(ServiceConnectionManager serviceManager) async {
_refreshShouldShowLeaksTab();

_memoryTracker = MemoryTracker(this);
_memoryTracker!.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'memory_graph_model.dart';
import 'memory_heap_treemap.dart';
import 'memory_instance_tree_view.dart';
import 'memory_snapshot_models.dart';
import 'panes/leaks/leaks_pane.dart';

const memorySearchFieldKeyName = 'MemorySearchFieldKey';

Expand Down Expand Up @@ -143,26 +144,16 @@ class HeapTreeViewState extends State<HeapTree>
static const dartHeapAnalysisTabKey = Key('Dart Heap Analysis Tab');
@visibleForTesting
static const dartHeapAllocationsTabKey = Key('Dart Heap Allocations Tab');
@visibleForTesting
static const leaksTabKey = Key('Leaks Tab');

/// Below constants should match index for Tab index in DartHeapTabs.
static const int analysisTabIndex = 0;
static const int allocationsTabIndex = 1;

static const _gaPrefix = 'memoryTab';

static final List<Tab> dartHeapTabs = [
DevToolsTab.create(
key: dartHeapAnalysisTabKey,
gaPrefix: _gaPrefix,
tabName: 'Analysis',
),
DevToolsTab.create(
key: dartHeapAllocationsTabKey,
gaPrefix: _gaPrefix,
tabName: 'Allocations',
),
];

late List<Tab> _tabs;
late TabController _tabController;

Widget? snapshotDisplay;
Expand Down Expand Up @@ -192,19 +183,47 @@ class HeapTreeViewState extends State<HeapTree>
void initState() {
super.initState();

_tabController = TabController(length: dartHeapTabs.length, vsync: this);
addAutoDisposeListener(_tabController);

_animation = _setupBubbleAnimationController();
}

void _initTabs() {
_tabs = [
DevToolsTab.create(
key: dartHeapAnalysisTabKey,
gaPrefix: _gaPrefix,
tabName: 'Analysis',
),
DevToolsTab.create(
key: dartHeapAllocationsTabKey,
gaPrefix: _gaPrefix,
tabName: 'Allocations',
),
if (widget.controller.shouldShowLeaksTab.value)
DevToolsTab.create(
key: leaksTabKey,
gaPrefix: _gaPrefix,
tabName: 'Leaks',
),
];

_tabController = TabController(length: _tabs.length, vsync: this);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
if (!initController()) return;

cancelListeners();

_initTabs();

addAutoDisposeListener(controller.shouldShowLeaksTab, () {
setState(() {
_initTabs();
});
});

addAutoDisposeListener(controller.selectedSnapshotNotifier, () {
setState(() {
controller.computeAllLibraries(rebuild: true);
Expand Down Expand Up @@ -418,7 +437,7 @@ class HeapTreeViewState extends State<HeapTree>
labelColor: themeData.textTheme.bodyText1!.color,
isScrollable: true,
controller: _tabController,
tabs: HeapTreeViewState.dartHeapTabs,
tabs: _tabs,
),
_buildSearchFilterControls(),
],
Expand Down Expand Up @@ -454,6 +473,10 @@ class HeapTreeViewState extends State<HeapTree>
],
),
),

// Leaks tab.
if (controller.shouldShowLeaksTab.value)
const KeepAliveWrapper(child: LeaksPane()),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Memory Leaks Tracking with DevTools

This page and functionality are under construction. See https://github.com/flutter/devtools/issues/3951.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';

class LeaksPane extends StatefulWidget {
const LeaksPane({Key? key}) : super(key: key);

@override
State<LeaksPane> createState() => _LeaksPaneState();
}

class _LeaksPaneState extends State<LeaksPane> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text('Memory leak tracking functionality will be here.'),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

library service_extensions;

import 'package:devtools_shared/devtools_shared.dart';
import 'package:flutter/material.dart';

import '../analytics/constants.dart' as analytics_constants;
Expand Down Expand Up @@ -526,9 +527,10 @@ final trackRebuildWidgets = ToggleableServiceExtensionDescription<bool>._(
gaItem: analytics_constants.trackRebuildWidgets,
);

// This extension should never be displayed as a button so does not need a
// ServiceExtensionDescription object.
// This extensions below should never be displayed as a button so does not need
// a ServiceExtensionDescription object.
const String didSendFirstFrameEvent = 'ext.flutter.didSendFirstFrameEvent';
const String memoryLeakTracking = memoryLeakTrackingExtensionName;

final List<ServiceExtensionDescription> _extensionDescriptions = [
debugPaint,
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_shared/lib/devtools_shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export 'src/memory/class_heap_detail_stats.dart';
export 'src/memory/event_sample.dart';
export 'src/memory/heap_sample.dart';
export 'src/memory/heap_space.dart';
export 'src/memory/leaks/constants.dart';
export 'src/memory/memory_json.dart';
export 'src/service_utils.dart';
2 changes: 2 additions & 0 deletions packages/devtools_shared/lib/src/memory/leaks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Code in this folder will be moved to Dart SDK and/or Flutter framework after piloting the feature.
See https://github.com/flutter/devtools/issues/3951.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// Name of extension that enables leak tracking for applications.
const memoryLeakTrackingExtensionName = 'ext.dart.memoryLeakTracking';