C # Performance Optimizations

Source: Internet
Author: User

1, using the Performance test Tool Dottrace 3.0, it can calculate the code in your program takes up more memory

2. Mandatory garbage collection
3, Multi-dispose,close
4, with a timer, every few seconds call: SetProcessWorkingSetSize (Process.getcurrentprocess (). Handle,-1,-1); see appendix for details.
5, release the time to choose release
6, pay attention to the code when writing less garbage, such as String + string will produce a lot of garbage, you can use Stringbuffer.append
7, this.    Dispose (); This.   Dispose (True); This.    Close (); Gc. Collect ();
8, pay attention to the scope of the variable, specifically, if a variable is only temporary use, do not define a member variable. GC is based on the network to recover resources.
9, detect if there is a memory leak, the details can be see: memory leaks Baidu Encyclopedia


Appendix: Periodic cleanup of garbage collection code:
In the program with a timer, every few seconds to call the function, open Task Manager, you will be surprised to find
#region Memory Recycling
[DllImport ("kernel32.dll", EntryPoint = "setprocessworkingsetsize")]
public static extern int SetProcessWorkingSetSize (INTPTR process, int minSize, int maxSize);
<summary>
Freeing memory
</summary>
public static void Clearmemory ()
{
Gc. Collect ();
Gc. WaitForPendingFinalizers ();
if (Environment.OSVersion.Platform = = PLATFORMID.WIN32NT)
{
SetProcessWorkingSetSize (System.Diagnostics.Process.GetCurrentProcess (). Handle,-1,-1);
}
}
#endregion

C # Performance Optimizations

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.