Parallel.For,ref一个自定义结构体(含HObject),内存会逐步增加,代码如下
- private void Test2()
- {
- int sum = 4000000;
- Result_test2 RT = new Result_test2();
- RT.Image = new HObject[sum];
- RT.Image2 = new HObject();
- HOperatorSet.GenEmptyObj(out RT.Image2);
- HOperatorSet.GenRectangle1(out RT.Image2, 100, 100, 2000, 2000);
- Parallel.For(0, sum, (int i) =>
- {
- _test2(ref RT, i);
- });
- RT.Image2.Dispose();
- for (int i = 0; i < sum; i++)
- {
- RT.Image[i].Dispose();
- }
- }
- private void _test2(ref Result_test2 RT, int index)
- {
- RT.Image[index] = new HObject();
- HOperatorSet.GenEmptyObj(out RT.Image[index]);
- HOperatorSet.GenRectangle1(out RT.Image[index], 100, 100, 2000, 2000);
- }
- public class Result_test2
- {
- public HObject[] Image;
- public HObject Image2;
- }
复制代码
读内存代码
- private string GetRam()
- {
- try
- {
- Process p = Process.GetCurrentProcess();
- if (p != null)
- {
- p.Refresh();
- double usedMemory = 0;
- string procName = p.ProcessName;
- using (PerformanceCounter pc = new PerformanceCounter("Process", "Working Set - Private", procName))
- {
- usedMemory = pc.NextValue() / 1024.0 / 1024.0;
- }
- return $" {usedMemory.ToString("F2")}MB";
- }
- return "GetRamEmpty";
- }
- catch (Exception ex)
- {
- return "GetRamError" + ex.Message;
- }
- }
复制代码 |