Asp.net26 A common performance optimization side

Source: Internet
Author: User
Tags add numbers config garbage collection memory usage thread

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.

Using Stored Procedures

A stored procedure is a set of precompiled SQL statements stored on a server, similar to a batch file in a DOS system. The stored procedure has the function of immediate access to the database, and the information processing is very rapid. By using stored procedures, you can avoid multiple compilations of commands, which reside in the cache after a single execution, and need to call only the binary code in the cache at a later time. In addition, the stored procedure runs on the server side, independent of the ASP.net program, easy to modify, and most importantly, it can reduce the transmission of database operation statements in the network.

Optimizing query Statements

Asp. NET, the ADO connection consumes a large amount of resources, the longer the SQL statement runs, the longer it takes to occupy the system resources. Therefore, try to use optimized SQL statements to reduce execution time. For example, do not include subqueries in query statements, make full use of indexes, and so on.

String Manipulation Performance optimization

ToString Method with value type

When connecting strings, you often add numbers directly to the string using the "+" number. Although this method is simple, it can get the correct result, but because it involves different data types, the number needs to be converted into a reference type by boxing operation to be added to the string. However, boxing operations have a greater impact on performance because, when such processing is done, a new object is allocated in the managed heap, and the original value is copied to the newly created object. Use the ToString method of value types to avoid boxing operations, thereby improving application performance.

Using the StringBuilder class

The string class object is immutable, and the method ToString does not significantly improve performance for a string object that is essentially recreating a string object and assigning the new value to the object. When working with strings, it is best to use the StringBuilder class, whose. NET namespace is system.text. Instead of creating a new object, the class operates directly on the string through methods such as Append,remove,insert, and returns the result of the operation through the ToString method.

Its definition and operation statements are as follows:

int num;

System.Text.StringBuilder str = new System.Text.StringBuilder (); Creating a String

Str. Append (Num. ToString ()); Add Numeric num

Response.Write (str. ToString); Show action Results

Optimize the configuration files for your WEB server computer and specific applications to meet your specific needs

By default, the ASP.net configuration is set to enable the broadest range of features and to accommodate the most common scenarios. As a result, application developers can optimize and change some of these configurations to improve application performance, depending on the functionality that the application uses. The following list is some of the options you should consider.

Enable authentication only for the applications that you want.

By default, the authentication mode is Windows, or integrated NTLM. In most cases, for applications that require authentication, it is a good idea to disable authentication in the Machine.config file and enable authentication in the Web.config file. Configure the application according to the appropriate request and response encoding settings. The asp.net default encoding format is UTF-8. If your application is strictly ASCII, configure your application to use ASCII for a slight performance boost.

Consider disabling AutoEventWireup for your application.

Setting the AutoEventWireup property to False in the Machine.config file means that the page does not match the method name to the event and hooks the two (for example, Page_Load). If page developers want to use these events, you need to override them in the base class (for example, you need to rewrite page.onload for page load events instead of using the Page_Load method). If AutoEventWireup is disabled, the page gets a slight performance boost by leaving the event connection to the page author instead of automatically executing it.

Remove unused modules from the request processing pipeline.

By default, all features of a node in the Machine.config file of the server computer are kept active. Depending on the functionality used by your application, you can remove unused modules from the request pipeline for a slight performance boost. Review each module and its functionality and customize it to your needs. For example, if you do not use session state and output caching in your application, you can remove them from the list so that requests do not have to perform entry and exit code for each module without performing other meaningful processing.

Be sure to disable debug mode

Always remember to disable debug mode before you deploy a production application or perform any performance measurement. If debug mode is enabled, the performance of your application can be significantly affected.

For applications that rely extensively on external resources, consider enabling network gardening on multiprocessor computers

The ASP.net process model helps enable scalability on multiprocessor computers, distribute work to multiple processes (one per CPU), and each process sets the processor relationship to its CPU. This technology is called Web gardening. If your application uses a slower database server or calls a COM object with external dependencies (here are just two possibilities), it is useful to enable Web gardening for your application. However, before you decide to enable network gardening, you should test the application's performance in the Web garden.

Cache data and page output whenever possible

ASP.net provides simple mechanisms that cache the page output or data when you do not need to dynamically compute page output or data for each page request. In addition, you can optimize the performance of these pages by designing the page and data requests that you want to cache, especially in areas where there is a large amount of traffic expected in the site. Using caching appropriately, compared to any WEB forms functionality of the. NET Framework, can improve the performance of your site, sometimes at a very high order of magnitude. There are two points to be aware of using the ASP.net caching mechanism. First, do not cache too many items. Caching each item has overhead, especially in terms of memory usage. Do not cache items that are easy to recalculate and rarely use. Second, the expiration of the cached items is not too short. Items that expire quickly can cause unnecessary turnover in the cache and often result in more code cleanup and garbage collection work. If you care about this issue, monitor the Cache total turnover Rate performance counters associated with the ASP.net applications performance object. High turnover may indicate a problem, especially if the item is removed before it expires. This is also called memory pressure.

Select the data viewing mechanism for the page or application

Depending on how you choose to display data on a WEB forms page, there is often an important trade-off between convenience and performance. For example, a DataGrid Web server control might be a quick and easy way to display data, but its overhead is often the largest in terms of performance. In some simple cases, you might be able to render data yourself by generating the appropriate HTML, but customization and browser orientation can quickly offset the extra benefits you get. Repeater Web server controls are a trade-off between convenience and performance. It is efficient, customizable and programmable.

To use the SqlDataReader class for fast forward-only data cursors

The SqlDataReader class provides a way to read a data-only stream retrieved from a SQL Server database. If you are creating a asp.net application that allows you to use it, the SqlDataReader class provides a higher performance than the DataSet class. This is because SqlDataReader uses SQL Server's native network data transfer format to read data directly from a database connection. In addition, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to a server control. For more information, see the SqlDataReader class. For information about how ASP.net accesses data, see Accessing data through asp.net.

Using SQL Server stored procedures for data access

In all the data access methods provided by the. NET Framework, SQL Server based data access is a recommended choice for generating high-performance, scalable WEB applications. When you use a managed SQL Server provider, you get additional performance gains by using compiled stored procedures instead of special queries.

Avoid single apartment (STA) COM components

By default, ASP.net does not allow any STA COM component to run on the page. To run them, you must include the Aspcompat=true property in the @ Page directive within the. aspx file. This switches the execution thread pool to the STA thread pool and makes HttpContext and other built-in objects available to COM objects. The former is also a performance optimization because it avoids any calls to marshal a multithreaded apartment (MTA) to an STA thread. Using an STA COM component can significantly impair performance and should be avoided as much as possible. If you must use an STA COM component, such as in any interop scheme, you should make a large number of calls during execution and send as much information as possible during each invocation. Also, be careful not to create any STA COM components during the construction of the page. For example, in the following code, the mystacomponent created by a thread is instantiated when the page is constructed, and the thread is not the STA thread that will run the page. This can have a detrimental effect on performance because the marshaling of the MTA and STA threads must be done to construct the page.

Related Article

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.