A program in the company, after the hands of N people found on the production of memory will continue to rise, until the physical memory is almost occupied after the sudden fall down (estimated that the GC is released), and then continue to rise. This program is mainly to deal with strings, troubled me for several days, the local test memory also has a trend of growth, but not obvious, on the production is 2, 3 minutes of memory on the G, did not find exactly where the problem.
From the Internet search some information, see can let. NET immediately free memory, the local simple test, really works, but did not dare to use, do not know for multi-threaded programs, when a thread shows the release of memory will not affect other threads, and I do the financial industry procedures, procedures in case of problems is an accident, so consult the experts, Does this code affect the stability of the program?
The code to free memory is as follows:
[DllImport ("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize (INTPTR process, int minSize, int maxSize);
public static void Garbagecollect ()
{
Gc. Collect ();
Gc. WaitForPendingFinalizers ();
Gc. Collect ();
}
public static void Flushmemory ()
{
Garbagecollect ();
if (Environment.OSVersion.Platform = = PLATFORMID.WIN32NT)
{
Win32.setprocessworkingsetsize (System.Diagnostics.Process.GetCurrentProcess (). Handle,-1,-1);
}
}
NET also has a class that can achieve this effect
System.Diagnostics.Process.GetCurrentProcess (). Minworkingset = new System.IntPtr (5);
With timed release + form open, the effect is very good, the estimated memory of the program will be less than 20