You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.cs 1.8 kB

10 years ago
10 years ago
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace Shadowsocks.Util
  9. {
  10. public class Utils
  11. {
  12. public static void ReleaseMemory()
  13. {
  14. // release any unused pages
  15. // making the numbers look good in task manager
  16. // this is totally nonsense in programming
  17. // but good for those users who care
  18. // making them happier with their everyday life
  19. // which is part of user experience
  20. GC.Collect(GC.MaxGeneration);
  21. GC.WaitForPendingFinalizers();
  22. SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
  23. (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF);
  24. }
  25. public static string UnGzip(byte[] buf)
  26. {
  27. byte[] buffer = new byte[1024];
  28. int n;
  29. using (MemoryStream sb = new MemoryStream())
  30. {
  31. using (GZipStream input = new GZipStream(new MemoryStream(buf),
  32. CompressionMode.Decompress, false))
  33. {
  34. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  35. {
  36. sb.Write(buffer, 0, n);
  37. }
  38. }
  39. return System.Text.Encoding.UTF8.GetString(sb.ToArray());
  40. }
  41. }
  42. [DllImport("kernel32.dll")]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. private static extern bool SetProcessWorkingSetSize(IntPtr process,
  45. UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize);
  46. }
  47. }