How to improve ASP. NET performance (1)

Source: Internet
Author: User
Tags net thread

If you write code in ASP. NET, you need to use the following to ensure good performance:

◆ Do you want to use cache?

◆ Are you using session status?

◆ Is the application status you are using?

◆ Are you using the thread and synchronization functions?

◆ What is the effective management of your resources?

◆ Are you effectively managing strings?

◆ Can you effectively manage exceptions?

◆ Have you optimized your webpage?

◆ Are you using view status?

◆ Do you use server controls?

◆ Do you access data from your page?

◆ Can you bind data?

◆ Do you call unmanaged code from the ASPX page?

◆ Have you reviewed the settings in Machine. config?

Do you use cache?

Use the following exercise to evaluate your code using the ASP. NET cache function:

◆ Do you have too many changes to the output cache?

Check your webpage and use the output cache to ensure that there is a limit on the number of changes. Too many changes to the output cache page can increase the memory usage. The webpage that you can recognize outputs the cache using the search string "OutputCache.

◆ Can you use the output cache?

When reviewing your webpage, start asking yourself: If you can cache the entire page. If the entire page cannot be cached, can it be cached? Even if the data is not static, you can consider using the output cache. If your content does not need to be transmitted in near real time, consider outputting the cache.

Using the output cache, whether the entire page or part of the page can significantly improve the performance.

◆ Yes. Will static data be better stored in the cache?

Identify whether the data at the application end is static or rarely updated. This type of data is stored in the cache as a great candidate.

◆ Do you check before the items in the cache are null?

Check the items with null access to the cache, as shown in the following code snippet, you can improve performance.

 
 
  1. Object item = Cache["myitem"];  
  2. if (item==null)  
  3. {  
  4.     // repopulate the cache  

This helps avoid any null objects that cause exceptions. To find your code in your access cache.

Are you using session status?

Use the following exercise to review the usage session Status of the Code:

◆ Do you need to disable the session status?

By default, the session status remains. If your application does not use the session Status, disable it in the Web. config file as follows:

If some parts of your application require session status and are sure not to use it, these pages disable it by using the following page-Level Attribute pages.

◆ Minimizes the session status used and increases the performance of applications.

◆ Do you have a page that does not write a session?

Page requests that use session Status use ReaderWriterLock internally to manage access to session status. For a webpage with read-only session data, set EnableSessionState to ReadOnly.

 
 
  1. <%@ Page EnableSessionState="ReadOnly" . . .%> 

This is especially useful when you use the HTML framework. The serialization is performed on the page by default (due to ReaderWriterLock. Set it to read-only to prevent blocking and allow more parallel operations.

◆ Do you check that the project in the previous session status is null?

Is empty, and then access the project check, as shown in the following code, you can improve performance.

 
 
  1. object item = Session["myitem"];  
  2. if(item==null)  
  3. {  
  4.     // do something else  

Common Errors are not checked when retrieving data from the session state. If the data is empty, access the data before, and then capture the exceptions. You should avoid this situation because exceptions are expensive. To find the Access session Status of your code, you can search for the string "meeting ".

◆ Is your complex object stored in the session state?

Avoid storing complex objects in the session state, especially if you use an out-of-process session state storage. When an out-of-process session state is used, each request of the object is serialized and deserialized, thus reducing performance.

◆ Is your sta com Object stored in the session state?

A single thread unit (STA) COM Object stored in the session state will cause thread association because the session is bound to the original thread to create this component. This seriously affects performance and scalability. Make sure that you use the following page-level attributes on any web page, and the sta com object is stored in the session state.

This force runs the stathread pool of the page to avoid any expensive apartment switch from the default multi-thread unit (MTA) to the ASP. NET thread pool. If possible, avoid using the sta com object.

Are you using the application status?

How to effectively evaluate the application status of your code using the following exercise:

◆ What is the status of your sta com component stored in the application?

Avoid the State where the sta com component is stored in the application when possible. This effectively bottlenecks your application when accessing a component from a separate execution thread. If possible, avoid using the sta com object.

◆ Is the application status dictionary used by you?

The value used to store read-only data can be set to the State Dictionary of the application that you should use after the application is initialized and does not change. You need to know the application status when using it in code, such as the following issues:

Memory allocated to the storage of Application variables is not released unless they are removed or replaced.

The application state is not a Web farm or a Web garden-variables shared in the application state are global specific application programs running ,. Each application process can have different values.

◆ Consider using the following alternative application status:

Static attributes created for applications rather than using status dictionaries. It is a more effective Dictionary of access status than a static attribute. For example, consider the following code:

This is more effective with the following code:

The configuration file is used to store application configuration information.

Considering that the cached data is sufficiently volatile, it cannot be stored in the application state, but it needs to regularly update the cached objects from a persistent medium.

Use sessions to store user-specific information. You can find out where your code is located and use the search string "application" app status.

Are you using the application status?

. NET Framework exposes various threads and synchronization functions. using multithreading in your code can significantly affect the performance and scalability of an application. Use the following exercises to effectively evaluate the threads used by your ASP. NET code:

◆ Are you creating a thread based on each request?

Avoid creating threads in ASP. NET Applications manually. Creating a thread is an expensive operation and requires initialization of hosted and unmanaged resources. If you need additional threads to execute the work, use the CLR thread pool. To locate your code, you are creating a thread and searching for the ThreadStart string ".

◆ Do you perform blocking operations that take long time to run?

Avoid blocking your ASP. NET application when possible. If you have to execute a long-running task, and then consider using asynchronous execution (if you can freely call the thread) or the asynchronous "fire, forget" model.

What about effective resource management?

Use the following review to assess how your code effectively uses resources:

Do you explicitly close the resource?

Make sure that your Code explicitly closes the object and implements the IDisposable interface by calling the Dispose or Close method of the object. The failure to close resources and speed can lead to increased memory consumption and poor performance.

Database connection is not closed, which is a common problem. Use finally blocks (or blocks used in C #) to release these resources to ensure that the resources are disabled, even if exceptions occur.

What about shared resources in the pool?

Check when you use the pool to access Shared resources to improve performance. Ensure that shared resources, such as database connections and service components, can be pooled and are being pooled. Without centralization, your code is initialized to overhead the usage time of each shared resource.

Do you get Resources late and release them early?

Before opening shared resources, you need them as long as you release them. Hold time than you need people, go to resources, increase the pressure on memory, and increase competition for these resources if they share.

Are you calling block data transfer I/O?

If you need to transfer data in block I/O calls, allocate and pin blocks for sending and receiving buffers. If you need to make concurrent I/O calls, you should create a fixed collection. This is done by creating a buffer pool based on different customers rather than each request. This helps you avoid heap fragmentation and reduce the buffer creation time.

How can you effectively manage strings?

Use the following exercise to evaluate how your ASP. NET code can manipulate strings effectively:

Is the Response. Write format output used by you?

Make sure that your code is output in your connection. For example, create a table and use Response. Write instead. The most effective way to reply to the client in writing.

Do you use StringBuilder to connect strings?

If the number of appends is unknown, you cannot send data to the client using Response. Write immediately. Use the StringBuilder class to connect strings.

Do you = connect string +?

Make sure that you use the + = Operator to concatenate strings in your code. If the number of appends is unknown or you append an unknown data size, use the StringBuilder class instead.

Can you effectively manage exceptions?

Use the following review questions to effectively evaluate usage exceptions in your code:

Have you implemented an error handler in Global. asax?

While implementing the error handler in Global. asax does not necessarily improve performance, it helps you identify your application in the event of unexpected exceptions. Take appropriate actions to avoid exceptions.

Are you using try/disposable resources?

Make sure to release the block in finally to make sure they are cleared, even when exceptions occur, disposable resources. Resources that are not disposed of are a common problem.

May I ask your code to avoid exceptions?

Your code should try to avoid exceptions to improve performance, because exceptions can cause significant overhead. Use the following methods:

Check for null values.

Do not use exceptions to control general application logic.

Do not capture exceptions. You cannot process and blur useful diagnostic information.

Reload Server. Transfer Method Server. Transfer (string, BOOL) instead of Server. Transfer, Response. Redirect, Response. End to avoid exceptions.

Are you optimizing your webpage?

Use the following exercise to evaluate the efficiency of the aspx page.

Do you take steps to reduce the page size?

Minimize the page size. The page size is large because of the increased processing capability and network bandwidth utilization, which may lead to a significant increase in network congestion and CPU load. These two factors lead to an increase in response time for customers. Consider the following guidelines to help reduce page size:

Including scripts (scripts, rather than HTML code tags ).

Remove unnecessary white space characters from your HTML.

Disable the view status of the server control.

Avoid long control names.

Minimize the number of images used, and use compressed images.

Use Cascading Style Sheets to prevent repeated sending of commands in the same format to the client.

Buffer disabled?

Make sure that you have enabled the buffer. Buffering will cause the server to buffer the output and send before processing the page. If buffering is disabled, the worker's process requires constant stream response from all concurrent requests, which may be significant overhead on memory and processor, especially when you use ASP. NET process model. To find out whether the buffer is disabled, you can search for the following strings for your code base: "buffer" and "BufferOutput ." Make sure that the <pages> element attribute of the buffer is set to true in the Web. config file of your application.

 
 
  1. <pages buffer="True"> 


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.