Skip to content

Fix memory leaks by cleaning up BrowserViewModel references #1148

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 21 commits into from
Apr 4, 2025

Conversation

BPerlakiH
Copy link
Collaborator

@BPerlakiH BPerlakiH commented Apr 3, 2025

Fixes: #1016

Many things had to change in order to clean up the BrowserViewModel references, this (rather large object) is holding a reference to the entire WKWebView for each tab, so it's important to clean it up properly once the tab is closed, otherwise we are using more and more memory.

Tested on iPhone, iPad and macOS.

@@ -282,6 +287,8 @@ struct RootView: View {
// but that's not comming from our window
return
}
windowTracker.current = nil // remove the reference to this window, see guard above
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a very non-obvious problem that was macOS specific

@@ -290,12 +297,9 @@ struct RootView: View {
let browser = BrowserViewModel.getCached(tabID: tabID)
// tab closed by user
browser.pauseVideoWhenNotInPIP()
Task { @MainActor [weak browser] in
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already called from navigation.deleteTab(tabID: tabID) a few lines below.

@@ -132,11 +132,24 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing by forcing SwiftUI body function switch between a browser based and non browser based views on a higher level, therefore we don't need to reference browserViewModel in the .loading state.

@@ -168,6 +177,10 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}
}

deinit {
debugPrint("🧨 BrowserViewModel deinit 🧨")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving this in, to have a clear indicator that the clean up is working, while we work on other features and change the code.

@codecov-commenter
Copy link

codecov-commenter commented Apr 3, 2025

Codecov Report

Attention: Patch coverage is 30.18018% with 155 lines in your changes missing coverage. Please review.

Project coverage is 24.13%. Comparing base (1b98031) to head (62b7660).

Files with missing lines Patch % Lines
Views/BrowserTab.swift 0.00% 40 Missing ⚠️
App/CompactViewController.swift 59.77% 35 Missing ⚠️
ViewModel/BrowserViewModel.swift 7.69% 12 Missing ⚠️
Views/Buttons/OutlineButton.swift 0.00% 11 Missing ⚠️
App/SplitViewController.swift 37.50% 10 Missing ⚠️
Views/Buttons/BookmarkButton.swift 25.00% 9 Missing ⚠️
ViewModel/NavigationViewModel.swift 0.00% 7 Missing ⚠️
Views/Commands.swift 0.00% 7 Missing ⚠️
Views/BuildingBlocks/ContentSearchButton.swift 0.00% 6 Missing ⚠️
App/App_iOS.swift 20.00% 4 Missing ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1148      +/-   ##
==========================================
- Coverage   25.09%   24.13%   -0.96%     
==========================================
  Files         136      136              
  Lines        7098     7179      +81     
==========================================
- Hits         1781     1733      -48     
- Misses       5317     5446     +129     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kelson42
Copy link
Contributor

kelson42 commented Apr 4, 2025

@BPerlakiH Inguess zou have remarked that the CI does not pass? Please ask for review only if the CI pass and keep the PR in draft as long as it does not.

Copy link
Member

@rgaudin rgaudin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! It looks good to me but it's mostly beyond my understanding…
Anyway it's such a big change that we need to keep this in mind for a while as there might be unforseen consequences in other places

@kelson42 kelson42 merged commit 3b45af0 into main Apr 4, 2025
8 checks passed
@kelson42 kelson42 deleted the browserViewModel-remove-references branch April 4, 2025 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix BrowserViewModel memory leak
4 participants