Home News How Can I Fix An Asp Net Mvc Memory Leak?

How Can I Fix An Asp Net Mvc Memory Leak?

70
0

 

In some cases, your system may generate an error stating that the net mvc asp. This error can be caused by a number of reasons.

 

 

My asp.net website reaches 2GB in 20 minutes and restarts.When I launch this website, I launch Visual Studio on localhost and refresh the page 30 times as the website grows by about 50-100 MB.I tried to run a very simple page with no database connection or markup. There was only one word on this page. And after several hundred updates to this page, the site has not grown.Then I tried to go back to the first (hard) page but commented out all the tags in the cshtml file. This page only had a layout. The site grewminor.Then I commented out the layout and left a simple table layout with a lot of input. After 30 updates, the site will grow again by about 50-100 MB.

asp net mvc memory leak

I tried to determine the cause using dotMemory. Some screenshots:

Tell me if I can provide you with more information.

Gen 2 fragmentation is 97%. (138 MB occupied, 220 free). I don’t know how it should be.

I took 2 pictures. Most of the new objects (and new bytes) are Char [], which is part of the StringBulder, which is part ofPageExecutionContext, which is part of the CacheEntry.

I guess there is a view cache (cshtml files). But this is just a guess.

Can anyone help me solve this problem?

What Type Of Memory Leak? Shared Memory = Managed Memory Unmanaged Memory

Before trying to understand the nature of the leak, let’s try to understand how memory is allocated in .NET applications. .NET applications have two types of managed memory and unmanaged memory. Managed memory is controlled by the garbage collector, and unmanaged memory is outside the limits of the garbage collector.

The first thing we need to do to make sure a memory leak is occurring is a managed leak or an unhandled leak. To determine if the leak has been repaired, two performance counters must be measured.

The first is the private application byte counter that we saw in the previous session.
The second counter we need to add is “bytes in all heaps”. Therefore, select .NET CLR memory in the performance object, select Bytes in All Heaps in the list of counters and select the application toThe second loses memory.


Private bytes are all memory used by the application. The bytes of all heaps are the memory used by the managed code. Thus, the equation takes the form shown in the following figure.

Unmanaged memory bytes in all helpers = private bytes. Therefore, if we want to find unmanaged memory, we can always subtract the bytes of all heaps from the private bytes.

We will now make two statements:

  • If private bytes are incremented and the bytes in all heaps remain constant, it means an unhandled memory leak.

  • If the bytes of all heaps grow linearly, this indicates a controlled memory leak.

Here is a typical screen shot of a raw leak. You can see that the private bytes are incremented while the bytes in the heaps remain constant

Here is a typical screen shot of a managed leak. The bytes of all heaps are incremented.

2. Registration Of Participants Using Anonymous Methods

While it may be obvious that the event-handling method means that the object is referenced, it is less obvious thatThe same is true when a class member is captured by an anonymous method.

Here’s an example:

In this code, the _id member is registered with the anonymous method, and the instance is also referenced accordingly. This means that JobQueue exists and refers to this worker delegate, but also refers to the MyClass instance.

The solution can be quite simple: assign a local variable:

By assigning a value to a local variable, nothing is saved and you avoid a possible memory leak.