@@ -14,6 +14,8 @@ namespace Shadowsocks.Controller | |||||
// manipulates UI | // manipulates UI | ||||
// interacts with low level logic | // interacts with low level logic | ||||
private Thread _ramThread; | |||||
private Local local; | private Local local; | ||||
private PACServer pacServer; | private PACServer pacServer; | ||||
private Configuration _config; | private Configuration _config; | ||||
@@ -58,6 +60,7 @@ namespace Shadowsocks.Controller | |||||
} | } | ||||
UpdateSystemProxy(); | UpdateSystemProxy(); | ||||
StartReleasingMemory(); | |||||
} | } | ||||
public Server GetCurrentServer() | public Server GetCurrentServer() | ||||
@@ -161,6 +164,8 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
ConfigChanged(this, new EventArgs()); | ConfigChanged(this, new EventArgs()); | ||||
} | } | ||||
Util.Util.ReleaseMemory(); | |||||
} | } | ||||
@@ -180,5 +185,21 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
UpdateSystemProxy(); | UpdateSystemProxy(); | ||||
} | } | ||||
private void StartReleasingMemory() | |||||
{ | |||||
_ramThread = new Thread(new ThreadStart(ReleaseMemory)); | |||||
_ramThread.IsBackground = true; | |||||
_ramThread.Start(); | |||||
} | |||||
private void ReleaseMemory() | |||||
{ | |||||
while (true) | |||||
{ | |||||
Util.Util.ReleaseMemory(); | |||||
Thread.Sleep(30 * 1000); | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
@@ -12,13 +12,13 @@ namespace Shadowsocks | |||||
{ | { | ||||
static class Program | static class Program | ||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
/// 应用程序的主入口点。 | /// 应用程序的主入口点。 | ||||
/// </summary> | /// </summary> | ||||
[STAThread] | [STAThread] | ||||
static void Main() | static void Main() | ||||
{ | { | ||||
Util.Util.ReleaseMemory(); | |||||
using (Mutex mutex = new Mutex(false, "Global\\" + "71981632-A427-497F-AB91-241CD227EC1F")) | using (Mutex mutex = new Mutex(false, "Global\\" + "71981632-A427-497F-AB91-241CD227EC1F")) | ||||
{ | { | ||||
Application.EnableVisualStyles(); | Application.EnableVisualStyles(); | ||||
@@ -41,6 +41,7 @@ namespace Shadowsocks | |||||
ShadowsocksController controller = new ShadowsocksController(); | ShadowsocksController controller = new ShadowsocksController(); | ||||
MenuViewController viewController = new MenuViewController(controller); | MenuViewController viewController = new MenuViewController(controller); | ||||
Util.Util.ReleaseMemory(); | |||||
Application.Run(); | Application.Run(); | ||||
} | } | ||||
} | } | ||||
@@ -0,0 +1,30 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Diagnostics; | |||||
using System.Runtime.InteropServices; | |||||
using System.Text; | |||||
namespace Shadowsocks.Util | |||||
{ | |||||
public class Util | |||||
{ | |||||
public static void ReleaseMemory() | |||||
{ | |||||
// release any unused pages | |||||
// making the numbers look good in task manager | |||||
// this is totally nonsense in programming | |||||
// but good for those users who care | |||||
// making them happier with their everyday life | |||||
// which is part of user experience | |||||
GC.Collect(GC.MaxGeneration); | |||||
GC.WaitForPendingFinalizers(); | |||||
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, | |||||
(UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); | |||||
} | |||||
[DllImport("kernel32.dll")] | |||||
[return: MarshalAs(UnmanagedType.Bool)] | |||||
private static extern bool SetProcessWorkingSetSize(IntPtr process, | |||||
UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize); | |||||
} | |||||
} |
@@ -299,8 +299,7 @@ namespace Shadowsocks.View | |||||
void configForm_FormClosed(object sender, FormClosedEventArgs e) | void configForm_FormClosed(object sender, FormClosedEventArgs e) | ||||
{ | { | ||||
configForm = null; | configForm = null; | ||||
GC.Collect(); | |||||
GC.WaitForPendingFinalizers(); | |||||
Util.Util.ReleaseMemory(); | |||||
ShowFirstTimeBalloon(); | ShowFirstTimeBalloon(); | ||||
} | } | ||||
@@ -84,6 +84,7 @@ | |||||
<Compile Include="Controller\PACServer.cs" /> | <Compile Include="Controller\PACServer.cs" /> | ||||
<Compile Include="Model\Server.cs" /> | <Compile Include="Model\Server.cs" /> | ||||
<Compile Include="Model\Configuration.cs" /> | <Compile Include="Model\Configuration.cs" /> | ||||
<Compile Include="Util\Util.cs" /> | |||||
<Compile Include="View\ConfigForm.cs"> | <Compile Include="View\ConfigForm.cs"> | ||||
<SubType>Form</SubType> | <SubType>Form</SubType> | ||||
</Compile> | </Compile> | ||||