asp.net C # implement manual Reclaim Memory implementation method

Source: Internet
Author: User

1. Assign null to object first;

The code is as follows Copy Code

2.system.gc.collect ();


Class Program
{
static void Main (string[] args)
{
Long Lenth = 1024 * 1024 * 128;

Getcost ("program start");

double[] data = new Double[lenth];
for (int i = 0; i < lenth; i++)
{
Data[i] = double. MaxValue;
}
Getcost ("Data manufacturing complete");

data = null;
Getcost ("data = null");

System.GC.Collect ();
Getcost ("System.GC.Collect ()");

Console.readkey ();
}

<summary>
Shows the status of memory usage
</summary>
<param name= "state" ></param>
static void Getcost (string state)
{
Console.Write ("Current status:" + state +); Occupy memory: ");
using (var p1 = new PerformanceCounter ("Process", "Working set-private", "Gctest.vshost"))
{
Console.WriteLine (P1. NextValue ()/1024/1024). ToString ("0.0") + "MB");
}
}
}

When you do not recycle manually, the system waits to be reclaimed at the end of the program execution. After using Data=null to indicate that the data is no longer in use, System.GC.Collect (), notify the system to do a recycle immediately, according to the C # memory management principle, the variables that are no longer used are returned


In fact, some of the methods summarized

Accessing a database resource requires creating a connection, opening a connection, and closing a connection several operations. These processes need to exchange information with the database more than once to authenticate and consume server resources. Asp. NET provides a connection pool (Connection pool) to improve the performance impact of opening and shutting down the database.   The system places the user's database connection in the connection pool, takes out when needed, recovers the connection when it is closed, and waits for the next connection request. The size of the connection pool is limited, and if the connection pool is maximized to create a connection, it will have a significant impact on performance. Therefore, after the database connection is established, the connection is opened only when the operation is really needed, closed immediately after use, so as to minimize the time to open the database connection, and avoid the occurrence of exceeding the connection limit. Use (recommended) using (

The code is as follows Copy Code
SqlConnection conn=new SqlConnection (ConnStr))
{
}
Do not have to show off or
try{
Conn. Open ();} Catch{}finally{conn. Close ();
}

And some netizens said

It is recommended that you use caching techniques

The code is as follows Copy Code

<%@ OutputCache duration= "180" varybyparam= "None"%>

This can slow down the pressure of the site, but also can periodically reclaim some of the memory resources


Data cache data caching is a powerful and very simple caching mechanism that allows you to save various objects for each application in a cache, which can be invoked based on HTTP requests, but are private in different applications. The data cache is implemented by the cache class. When an application is established, a cache class is created at the same time, and the lifetime of the cached instance is the lifecycle of the application, which is rebuilt as the application is rerun, and through the method of the cache class, we can put the data objects into the buffer and then search for and use the objects by keyword matching. The cache class uses an excuse to control all content that needs to be cached, including the timing and caching of the cache,
You can add a cached object by: cache["keyword" = value of the keyword, and then access the object by using the following method:

  code is as follows copy code

String Mkeyvalue = "";
if (cache["keyword"]!= null)
{
Mkeyvalue = cache["keyword"];
}

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.