Aggressive GC after window closed

This commit is contained in:
Paddy Xu
2017-05-28 15:59:31 +03:00
parent 7e95d5ea2b
commit 3768470a33
4 changed files with 38 additions and 18 deletions

View File

@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;
namespace QuickLook.Helpers
{
internal class ProcessHelper
{
// ReSharper disable once InconsistentNaming
public static void PerformAggressiveGC()
{
// delay some time to make sure that all windows are closed
Task.Delay(1000).ContinueWith(t => GC.Collect(GC.MaxGeneration));
}
}
}

View File

@@ -4,10 +4,12 @@ using System.IO;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Windows; using System.Windows;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading; using System.Windows.Threading;
using QuickLook.Helpers; using QuickLook.Helpers;
using QuickLook.Helpers.BlurLibrary; using QuickLook.Helpers.BlurLibrary;
using QuickLook.Plugin; using QuickLook.Plugin;
using KeyEventArgs = System.Windows.Forms.KeyEventArgs;
namespace QuickLook namespace QuickLook
{ {
@@ -81,15 +83,12 @@ namespace QuickLook
internal void UnloadPlugin() internal void UnloadPlugin()
{ {
// clear ref to control
//container.Content = null;
ContextObject.Reset(); ContextObject.Reset();
_plugin?.Cleanup(); _plugin?.Cleanup();
_plugin = null; _plugin = null;
GC.Collect(); ProcessHelper.PerformAggressiveGC();
} }
internal void BeginShow(IViewer matchedPlugin, string path) internal void BeginShow(IViewer matchedPlugin, string path)
@@ -97,6 +96,8 @@ namespace QuickLook
_path = path; _path = path;
_plugin = matchedPlugin; _plugin = matchedPlugin;
ContextObject.ViewerWindow = this;
// get window size before showing it // get window size before showing it
_plugin.Prepare(path, ContextObject); _plugin.Prepare(path, ContextObject);
@@ -150,8 +151,14 @@ namespace QuickLook
internal void BeginHide() internal void BeginHide()
{ {
// the focused element will not processed by GC: https://stackoverflow.com/questions/30848939/memory-leak-due-to-window-efectivevalues-retention
FocusManager.SetFocusedElement(this, null);
Keyboard.ClearFocus();
UnloadPlugin(); UnloadPlugin();
Hide(); Hide();
ProcessHelper.PerformAggressiveGC();
} }
} }
} }

View File

@@ -8,21 +8,17 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace QuickLook.Properties namespace QuickLook.Properties {
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.1.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default public static Settings Default {
{ get {
get
{
return defaultInstance; return defaultInstance;
} }
} }

View File

@@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<RootNamespace>QuickLook</RootNamespace> <RootNamespace>QuickLook</RootNamespace>
<AssemblyName>QuickLook</AssemblyName> <AssemblyName>QuickLook</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
@@ -28,6 +28,7 @@
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<ApplicationIcon>Resources\app.ico</ApplicationIcon> <ApplicationIcon>Resources\app.ico</ApplicationIcon>
@@ -108,6 +109,7 @@
<Compile Include="Helpers\BlurLibrary\PlatformsImpl\Windows8WindowBlurController.cs" /> <Compile Include="Helpers\BlurLibrary\PlatformsImpl\Windows8WindowBlurController.cs" />
<Compile Include="Helpers\BlurLibrary\PlatformsImpl\WindowsVistaWindowBlurController.cs" /> <Compile Include="Helpers\BlurLibrary\PlatformsImpl\WindowsVistaWindowBlurController.cs" />
<Compile Include="Helpers\FileHelper.cs" /> <Compile Include="Helpers\FileHelper.cs" />
<Compile Include="Helpers\ProcessHelper.cs" />
<Compile Include="MainWindowNoTransparent.cs" /> <Compile Include="MainWindowNoTransparent.cs" />
<Compile Include="NativeMethods\Shell32\IShellLink.cs" /> <Compile Include="NativeMethods\Shell32\IShellLink.cs" />
<Compile Include="NativeMethods\Shell32\IWshShell.cs" /> <Compile Include="NativeMethods\Shell32\IWshShell.cs" />