Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: High CPU Utilization in Web Service or Web Form


View products that this article applies to.

This article was previously published under Q307340
This article refers to the Microsoft .NET Framework Class Library System.Text namespace.

↑ Back to the top


Symptoms

You may notice that your production Web server has 100 percent CPU utilization and slow response times, even under light load.

↑ Back to the top


Cause

This problem frequently occurs when your application performs many string concatenations, such as dynamically building HTML or XML strings. In this scenario, Aspnet_wp.exe (or W3wp.exe , for applications that run on IIS 6.0) is the offending process. You can see evidence of this problem in Task Manager on the Processes tab or in the Performance Monitor counter.

↑ Back to the top


Resolution

Use one of the following methods to resolve this problem.

Solution 1

Use the System.Text.StringBuilder class to perform the concatenations. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
306821� HOW TO: Improve String Concatenation Performance in Visual Basic .NET

Solution 2

Use the Response.Write method (for Web Forms) instead of concatenation to stream the text.

Note This solution assumes that your code is in ASP-style render blocks. For example:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="mypage.aspx.vb" Inherits="myproject._mypage"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
   <body>
      <form id="Form1" method="post" runat="server">
         <asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
         <p></p>
         <% 
            Response.Write("My ") 
            Response.Write("streamed ")
            Response.Write("text")
         %>
         <p></p>
      <asp:Button id="Button2" runat="server" Text="Button2"></asp:Button>
      </form>
   </body>
</HTML>
				

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More information

String concatenation is notoriously inefficient. It involves character copies on the order of "l * n * n," where "l" is the average string length, and "n" is the number of substrings that are concatenated together. When "n" is a large value, your application can dramatically slow, and the concatenation can exhaust both the CPU and the heap. StringBuilder efficiency is on the order of "n * l," if you can reasonably estimate the buffer size in advance. StringBuilder is somewhat less efficient if it needs to grow the internal buffer but is still much better than concatenation.

↑ Back to the top


Keywords: KB307340, kbwebforms, kbprb, kbperformance

↑ Back to the top

Article Info
Article ID : 307340
Revision : 10
Created on : 8/24/2005
Published on : 8/24/2005
Exists online : False
Views : 618