DanWBR / dwsim

DWSIM is a Steady-State and Dynamic Sequential Modular Chemical Process Simulator for Windows, Linux and macOS.
https://dwsim.org
GNU General Public License v3.0
300 stars 96 forks source link

[Bug] .NET Automation3 via COM gradual slowness (a memory leak?) #664

Closed polomani closed 3 months ago

polomani commented 4 months ago

Describe the bug I noticed Automation3 getting very slow over time. I ran the script below, which opens the same model over and over and tries to run CalculateFlowsheet2 on it. It takes the same amount of time to open the model each time (except the very first), but it gets a couple of % slower to solve it. If I run the process for hours, solving the flowsheet eventually takes 20+ seconds.

To Reproduce

Any simplest flowsheet, E.g.: DWSIM-ShowerMixer-v1.dwxmz.zip

  1. Run the C# code

    for (int i = 0; i < 1000; i++)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            var serverType = Type.GetTypeFromProgID("DWSIM.Automation.Automation3");
            dynamic server = Activator.CreateInstance(serverType);
            var model = server.LoadFlowsheet("./ShowerMixer-v1.dwxmz");
            watch.Stop();
            var watch2 = System.Diagnostics.Stopwatch.StartNew();
            server.CalculateFlowsheet2(model);
            watch2.Stop();
    
            if (!model.Solved)
            {
                Console.WriteLine("Model not solved");
                break;
            }
            if (model != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(model);
            }
            if (server != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(server);
            }
            Console.WriteLine($"Model opened in {watch.ElapsedMilliseconds} ms. Solved {watch2.ElapsedMilliseconds}");
        }

Expected behavior The CalculateFlowsheet2() should take the same amount of time on the same model.

Output

Model loaded in 6808 ms. Solved in 780
Model loaded in 1099 ms. Solved in 8
Model loaded in 1076 ms. Solved in 8
Model loaded in 891 ms. Solved in 7
Model loaded in 928 ms. Solved in 13
Model loaded in 880 ms. Solved in 9
Model loaded in 1009 ms. Solved in 11
Model loaded in 1245 ms. Solved in 12
Model loaded in 1074 ms. Solved in 11
Model loaded in 1064 ms. Solved in 18
Model loaded in 893 ms. Solved in 16
Model loaded in 883 ms. Solved in 13
Model loaded in 844 ms. Solved in 12
Model loaded in 822 ms. Solved in 15
Model loaded in 991 ms. Solved in 16
Model loaded in 1081 ms. Solved in 18
Model loaded in 1008 ms. Solved in 18
Model loaded in 987 ms. Solved in 18
Model loaded in 877 ms. Solved in 20
Model loaded in 837 ms. Solved in 19
Model loaded in 819 ms. Solved in 18
Model loaded in 923 ms. Solved in 19
Model loaded in 985 ms. Solved in 96
Model loaded in 1067 ms. Solved in 25
Model loaded in 1103 ms. Solved in 38
Model loaded in 1156 ms. Solved in 36
Model loaded in 1114 ms. Solved in 35

Desktop (please complete the following information):

Greg66 commented 3 months ago

I encountered the same problem even without using Automation. I executed a flowsheet several 100 times with minute modifications to adjust several parameters in parallel. After a while DWSIM is getting slower and slower. I saved die simulation, closed it and started again. After restart i got the original speed again.

DanWBR commented 3 months ago

@polomani why are you using COM instead of calling .NET types and objects directly?

polomani commented 3 months ago

@DanWBR I use COM to connect to other applications also. This allows writing a more generic middleware code (where possible) as well as avoiding bundling all the different DLLs into the release artifact.

Is there a known issue with COM?