peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

memory grow day by day,gc mode can not work in long time background console project #1033

Closed avriltank closed 2 years ago

avriltank commented 2 years ago

gc1 gc2 gc3

I have try to change gc mod,there is no effect.

    <ServerGarbageCollection>false</ServerGarbageCollection>
    <ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>

The .net Gc mod is very magic,same php code only takes 50m memory size,.net code takes 200m memory size.

avriltank commented 2 years ago
        [DllImport("psapi.dll")]
        private static extern int EmptyWorkingSet(int hProcess);

       public void GCFree()
       {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    try
                    {
                        EmptyWorkingSet(System.Diagnostics.Process.GetCurrentProcess()
                                .Handle.ToInt32());
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
      }

We can free memory by ourself,the raw gc behavior only frees the 2rd and 3rd resource when the memory size becomes the .net setted size.(this is the offical url:https://docs.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector)