Microsoft KB Archive/307340

= PRB: High CPU Utilization in Web Service or Web Form =

Article ID: 307340

Article Last Modified on 8/24/2005

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Web Services Enhancements for Microsoft .NET 2.0

-



This article was previously published under Q307340



This article refers to the Microsoft .NET Framework Class Library System.Text namespace.



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



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.



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=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;mypage.aspx.vb&quot; Inherits=&quot;myproject._mypage&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>    <%            Response.Write(&quot;My &quot;) Response.Write(&quot;streamed &quot;) Response.Write(&quot;text&quot;) %>      



STATUS
This behavior is by design.



MORE INFORMATION
String concatenation is notoriously inefficient. It involves character copies on the order of &quot;l * n * n,&quot; where &quot;l&quot; is the average string length, and &quot;n&quot; is the number of substrings that are concatenated together. When &quot;n&quot; 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 &quot;n * l,&quot; 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.

Additional query words: 100% CPU

Keywords: kbperformance kbwebforms kbprb KB307340

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.