asp.net.netmemory-leaksasp.net-customcontrol

ASP .NET web application memory leaks - profiler shows a lot of strings


My web application constantly hits the IIS limitation set on the virtual memory allocated to the application pool. This causes IIS to stop the application.

I've been trying to identify possible memory leaks in my app using .NET memory profiler and so far the largest amount of memory retained after GC seems to be in strings. Even for accessing one page the memory usage grows quite a lot.

When I look into the strings stored I find duplicated strings like SQL queries used in SqlDataSource.SelectCommand

My website consists of a masterpage in which I have some user controls. In one of these user controls I use an SqlDataSource that does a simple select from the database like this:

<asp:SqlDataSource DataSourceMode="DataSet" CacheDuration="100" ID="myDataSource"
    runat="server" ProviderName="System.Data.Odbc" ConnectionString="<%$ ConnectionStrings:mysql %>"></asp:SqlDataSource>

In the control's Page_Load I have:

myDataSource.SelectCommand =
            "SELECT * from table limit 0,10";

The same string I can find duplicated in memory hundreds of times (probably each time the page is accessed).

Am I missing something? Do I have to dispose the data source manually?

thanks

UPDATE:

Turns out that a huge amount of strings (including the SQL queries) were kept in memory because I had a several custom user controls which apparently use by default EnableViewState="true"

After setting EnableViewState="false" for these controls (I did not need it) and the strings are not longer filling up the memory

UPDATE 1:

After setting EnableViewState="false" in production the application pool no longer hits the virtual memory limitation, so PROBLEM SOLVED!


Solution

  • Turns out that a huge amount of strings (including the SQL queries) were kept in memory because I had a several custom user controls which apparently use by default EnableViewState="true"

    After setting EnableViewState="false" for these controls (I did not need it) and the strings are not longer filling up the memory

    UPDATE 1:

    After setting EnableViewState="false" in production the application pool no longer hits the virtual memory limitation, so PROBLEM SOLVED!

    This article goes in-depth explaining what's going on: https://blogs.msdn.microsoft.com/tess/2008/09/09/asp-net-memory-identifying-pages-with-high-viewstate/ (thanks Lex Li)