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.

MemoryMonitor.cs 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using static Tensorflow.Binding;
  6. namespace Tensorflow
  7. {
  8. public class MemoryMonitor
  9. {
  10. public void WarmUp()
  11. {
  12. print(tf.VERSION);
  13. }
  14. public void Execute(int epoch, int iterate, Action<int> process)
  15. {
  16. /*GC.Collect();
  17. GC.WaitForPendingFinalizers();
  18. GC.Collect();*/
  19. print($"{process.Method.Name} started...");
  20. for (int i = 0; i < epoch; i++)
  21. {
  22. var initialMemory = Process.GetCurrentProcess().PrivateMemorySize64;// GC.GetTotalMemory(true);
  23. process(iterate);
  24. var finalMemory = Process.GetCurrentProcess().PrivateMemorySize64; //GC.GetTotalMemory(true);
  25. print($"Epoch {i}: {Format(finalMemory - initialMemory)}.");
  26. }
  27. GC.Collect();
  28. GC.WaitForPendingFinalizers();
  29. GC.Collect();
  30. print($"Total {process.Method.Name} usage {Format(Process.GetCurrentProcess().PrivateMemorySize64)}");
  31. }
  32. private string Format(long usage)
  33. {
  34. if (usage < 0)
  35. return $"-{Format(0 - usage)}";
  36. if (usage <= 1024 && usage >= 0)
  37. return $"{usage} Bytes";
  38. else if (usage > 1024 && usage <= 1024 * 1024)
  39. return $"{usage / 1024} KB";
  40. else
  41. return $"{usage / 1024 / 1024} MB";
  42. }
  43. }
  44. }