release memory periodically

This commit is contained in:
clowwindy
2014-11-18 21:55:45 +08:00
parent 919f9b5ce3
commit 09ad2ca272
5 changed files with 55 additions and 3 deletions
@@ -14,6 +14,8 @@ namespace Shadowsocks.Controller
// manipulates UI
// interacts with low level logic
private Thread _ramThread;
private Local local;
private PACServer pacServer;
private Configuration _config;
@@ -58,6 +60,7 @@ namespace Shadowsocks.Controller
}
UpdateSystemProxy();
StartReleasingMemory();
}
public Server GetCurrentServer()
@@ -161,6 +164,8 @@ namespace Shadowsocks.Controller
{
ConfigChanged(this, new EventArgs());
}
Util.Util.ReleaseMemory();
}
@@ -180,5 +185,21 @@ namespace Shadowsocks.Controller
{
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);
}
}
}
}
+2 -1
View File
@@ -12,13 +12,13 @@ namespace Shadowsocks
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Util.Util.ReleaseMemory();
using (Mutex mutex = new Mutex(false, "Global\\" + "71981632-A427-497F-AB91-241CD227EC1F"))
{
Application.EnableVisualStyles();
@@ -41,6 +41,7 @@ namespace Shadowsocks
ShadowsocksController controller = new ShadowsocksController();
MenuViewController viewController = new MenuViewController(controller);
Util.Util.ReleaseMemory();
Application.Run();
}
}
+30
View File
@@ -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)
{
configForm = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Util.Util.ReleaseMemory();
ShowFirstTimeBalloon();
}
@@ -84,6 +84,7 @@
<Compile Include="Controller\PACServer.cs" />
<Compile Include="Model\Server.cs" />
<Compile Include="Model\Configuration.cs" />
<Compile Include="Util\Util.cs" />
<Compile Include="View\ConfigForm.cs">
<SubType>Form</SubType>
</Compile>