Ways to let. NET programs quickly free up memory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.