Browse Source

release memory periodically

tags/3.2
clowwindy 10 years ago
parent
commit
09ad2ca272
5 changed files with 55 additions and 3 deletions
  1. +21
    -0
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  2. +2
    -1
      shadowsocks-csharp/Program.cs
  3. +30
    -0
      shadowsocks-csharp/Util/Util.cs
  4. +1
    -2
      shadowsocks-csharp/View/MenuViewController.cs
  5. +1
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 21
- 0
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -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
shadowsocks-csharp/Program.cs 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
- 0
shadowsocks-csharp/Util/Util.cs 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);
}
}

+ 1
- 2
shadowsocks-csharp/View/MenuViewController.cs View File

@@ -299,8 +299,7 @@ namespace Shadowsocks.View
void configForm_FormClosed(object sender, FormClosedEventArgs e)
{
configForm = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Util.Util.ReleaseMemory();
ShowFirstTimeBalloon();
}


+ 1
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -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>


Loading…
Cancel
Save