using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Runtime.InteropServices; using System.Text; namespace Shadowsocks.Util { public class Utils { 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); } public static string UnGzip(byte[] buf) { byte[] buffer = new byte[1024]; int n; using (MemoryStream sb = new MemoryStream()) { using (GZipStream input = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress, false)) { while ((n = input.Read(buffer, 0, buffer.Length)) > 0) { sb.Write(buffer, 0, n); } } return System.Text.Encoding.UTF8.GetString(sb.ToArray()); } } [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetProcessWorkingSetSize(IntPtr process, UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize); } }