optimize<*>: reduce memory usage

This commit is contained in:
leo 2024-02-20 18:27:59 +08:00
parent ca19d65f0e
commit 57c4a8394c
8 changed files with 99 additions and 16 deletions

View file

@ -114,6 +114,24 @@ namespace SourceGit.ViewModels {
_repo = repo;
}
public void Cleanup() {
_repo = null;
_commit = null;
if (_changes != null) _changes.Clear();
if (_visibleChanges != null) _visibleChanges.Clear();
if (_changeTree != null) _changeTree.Clear();
_selectedChange = null;
_selectedChangeNode = null;
_searchChangeFilter = null;
_diffContext = null;
if (_revisionFiles != null) _revisionFiles.Clear();
if (_revisionFilesTree != null) _revisionFilesTree.Clear();
_selectedRevisionFileNode = null;
_searchFileFilter = null;
_viewRevisionFileContent = null;
_cancelToken = null;
}
public void NavigateTo(string commitSHA) {
var repo = Preference.FindRepository(_repo);
if (repo != null) repo.NavigateToCommit(commitSHA);

View file

@ -56,6 +56,22 @@ namespace SourceGit.ViewModels {
_repo = repo;
}
public void Cleanup() {
Commits = new List<Models.Commit>();
_repo = null;
_graph = null;
_autoSelectedCommit = null;
if (_detailContext is CommitDetail cd) {
cd.Cleanup();
} else if (_detailContext is RevisionCompare rc) {
rc.Cleanup();
}
_detailContext = null;
}
public void NavigateTo(string commitSHA) {
var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA));
if (commit != null) {

View file

@ -1,5 +1,8 @@
using Avalonia.Collections;
using Avalonia.Markup.Xaml.MarkupExtensions;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.IO;
namespace SourceGit.ViewModels {
@ -74,8 +77,6 @@ namespace SourceGit.ViewModels {
LauncherPage page = param as LauncherPage;
if (page == null) page = _activePage;
CloseRepositoryInTab(page);
var removeIdx = Pages.IndexOf(page);
var activeIdx = Pages.IndexOf(_activePage);
if (removeIdx == activeIdx) {
@ -85,31 +86,37 @@ namespace SourceGit.ViewModels {
ActivePage = Pages[removeIdx + 1];
}
CloseRepositoryInTab(page);
Pages.RemoveAt(removeIdx);
OnPropertyChanged(nameof(Pages));
} else if (removeIdx + 1 == activeIdx) {
CloseRepositoryInTab(page);
Pages.RemoveAt(removeIdx);
OnPropertyChanged(nameof(Pages));
} else {
CloseRepositoryInTab(page);
Pages.RemoveAt(removeIdx);
}
GC.Collect();
}
public void CloseOtherTabs(object param) {
if (Pages.Count == 1) return;
LauncherPage page = param as LauncherPage;
var page = param as LauncherPage;
if (page == null) page = _activePage;
ActivePage = page;
foreach (var one in Pages) {
if (one.Node.Id != page.Node.Id) {
CloseRepositoryInTab(one);
}
if (one.Node.Id != page.Node.Id) CloseRepositoryInTab(one);
}
ActivePage = page;
Pages = new AvaloniaList<LauncherPage> { page };
OnPropertyChanged(nameof(Pages));
GC.Collect();
}
public void CloseRightTabs(object param) {
@ -126,6 +133,8 @@ namespace SourceGit.ViewModels {
CloseRepositoryInTab(Pages[i]);
Pages.Remove(Pages[i]);
}
GC.Collect();
}
public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page) {
@ -164,14 +173,16 @@ namespace SourceGit.ViewModels {
}
private void CloseRepositoryInTab(LauncherPage page) {
if (!page.Node.IsRepository) return;
if (page.Node.IsRepository) {
var repo = Preference.FindRepository(page.Node.Id);
if (repo == null) return;
if (repo != null) {
Commands.AutoFetch.RemoveRepository(repo.FullPath);
repo.Close();
}
}
page.View = null;
}
private LauncherPage _activePage = null;
}

View file

@ -189,12 +189,17 @@ namespace SourceGit.ViewModels {
}
public void Close() {
SelectedView = 0.0; // Do NOT modify. Used to remove exists widgets for GC.Collect
_watcher.Dispose();
_histories.Cleanup();
_workingCopy.Cleanup();
_stashesPage.Cleanup();
_watcher = null;
_histories = null;
_workingCopy = null;
_stashesPage = null;
_selectedView = null;
_isSearching = false;
_searchCommitFilter = string.Empty;
@ -205,8 +210,6 @@ namespace SourceGit.ViewModels {
_tags.Clear();
_submodules.Clear();
_searchedCommits.Clear();
GC.Collect();
}
public void OpenInFileManager() {

View file

@ -96,6 +96,17 @@ namespace SourceGit.ViewModels {
});
}
public void Cleanup() {
_repo = null;
if (_changes != null) _changes.Clear();
if (_visibleChanges != null) _visibleChanges.Clear();
if (_changeTree != null) _changeTree.Clear();
_selectedChange = null;
_selectedNode = null;
_searchFilter = null;
_diffContext = null;
}
public void NavigateTo(string commitSHA) {
var repo = Preference.FindRepository(_repo);
if (repo != null) repo.NavigateToCommit(commitSHA);

View file

@ -67,6 +67,15 @@ namespace SourceGit.ViewModels {
_repo = repo;
}
public void Cleanup() {
_repo = null;
if (_stashes != null) _stashes.Clear();
_selectedStash = null;
if (_changes != null) _changes.Clear();
_selectedChange = null;
_diffContext = null;
}
public void Apply(object param) {
if (param is Models.Stash stash) {
Task.Run(() => {

View file

@ -78,6 +78,17 @@ namespace SourceGit.ViewModels {
_repo = repo;
}
public void Cleanup() {
_repo = null;
if (_unstaged != null) _unstaged.Clear();
if (_staged != null) _staged.Clear();
if (_unstagedTree != null) _unstagedTree.Clear();
if (_stagedTree != null) _stagedTree.Clear();
_lastViewChange = null;
_detailContext = null;
_commitMessage = string.Empty;
}
public bool SetData(List<Models.Change> changes) {
var unstaged = new List<Models.Change>();
var staged = new List<Models.Change>();

View file

@ -466,6 +466,10 @@
<DataTemplate DataType="vm:StashesPage">
<v:StashesPage/>
</DataTemplate>
<DataTemplate DataType="x:Double">
<Border/>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</Grid>