How to limit memory usage for asp.net? RRS feed

  • Question

  • User-1808204847 posted

    Our web-server has multiple instances of an asp.net web-forms site that use too much memory. The w3wp.exe processes take memory until the machine is near 100% of physical ram and then starts using cpu.

    I have tried:

    Is there any way to restrict how much memory an asp.net website can use? If not how can we see what is using the most memory? How can we keep memory usage in check? Thank you.

    Thursday, February 21, 2019 8:20 PM

All replies

  • User1120430333 posted

    Is the solution using n-tier and seperation of concerns with presentation tier, business tier and data access tier on seperate machines? Is the solution using datatable that is a resource hog? Is the application disposing of objects using the 'using' statement? 

    Thursday, February 21, 2019 9:00 PM
  • User475983607 posted

    Is there any way to restrict how much memory an asp.net website can use?

    The Application Pool's Private Memory Limit restricts the amount of memory the worker process can use before restarting.  You can find this setting in IIS Application Pools -> Advanced Settings.

    If not how can we see what is using the most memory?

    See the Performance Monitor.


    How can we keep memory usage in check?

    There's no one size fits all solutions.  It sounds like a memory leak if the applications are not releasing memory.  That's a programming bug that you need to find.

    Thursday, February 21, 2019 9:18 PM
  • User753101303 posted


    Which VS edition do you have ? I used https://docs.microsoft.com/en-us/visualstudio/debugger/using-dump-files?view=vs-2017 once and in particular the "Debug Managed Memory" option. It shows the count and size for each type of object found un the dump which should help to narrow down what happens. Unfortunately it is not available in the Community Edition (maybe Pro if not Ultimate).

    If using EF, try perhaps to trace how many objects are loaded in the context (maybe EF queries loading more data than needed causing an excessive memory consumption and garbage collection actiivity).

    Thursday, February 21, 2019 9:30 PM
  • User1341756031 posted

    When a .NET application starts, it asks the OS for a chunk of memory which it then segments to become the managed heap, stack, and large object heap. It is this total chunk of memory that TaskManager is reporting, which may or may not be completely used by .NET. Once a .NET application is given a chunk of memory, it will not release it until asked by the OS, which will only happen with the OS determines a need for more memory resources.

    .NET will manage garbage collection for you very efficiently. While on types that implement IDisposable it is wise to call the Dispose method, this probably isn't your problem. Memory leaks can happen in .NET for a lot of reasons. Here are a few:

    • You cache too much data per user in the Session.
    • You cache too much data on an application level in the application cache or in a static variable such as a dictionary.
    • You store web controls (or user controls) in the session or application level.
    • You hook instances to events on static types or to types that keep being referenced (because they are stored in a cache).

    I hope this gives you some ideas about where to look.

    Monday, February 25, 2019 6:11 AM
  • User1464422028 posted

    I also looking for how to optimize memory usage with IIS.

    Anyway, you can set iis max memory usage in machine.config. The default is <processModel autoConfig="true"/> which is max 60% of memory. And you can override the memory limit with memoryLimit property.

    "Configures the ASP.NET process model settings on a Microsoft Internet Information Services (IIS) Web server. The processModel section can be set only within the Machine.config file and affects all ASP.NET applications that are running on the server." -- processModel Element (ASP.NET Settings Schema)

    Wednesday, December 11, 2019 6:16 PM